test.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python3
  2. #
  3. # test.py -- Unit tests for jpegoptim
  4. #
  5. # Copyright (C) 2023-2025 Timo Kokkonen <tjko@iki.fi>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. """jpegoptim unit tester"""
  21. import os
  22. import subprocess
  23. import unittest
  24. class JpegoptimTests(unittest.TestCase):
  25. """jpegoptim test cases"""
  26. program = '../jpegoptim'
  27. debug = False
  28. def setUp(self):
  29. if "JPEGOPTIM" in os.environ:
  30. self.program = os.environ["JPEGOPTIM"]
  31. if "DEBUG" in os.environ:
  32. self.debug = True
  33. def run_test(self, args, check=True, directory=None):
  34. """execute jpegoptim for a test"""
  35. command = [self.program] + args
  36. if directory:
  37. if not os.path.isdir(directory):
  38. os.makedirs(directory)
  39. command.extend(['-o', '-d', directory])
  40. if self.debug:
  41. print(f'\nRun command: {" ".join(command)}')
  42. res = subprocess.run(command, encoding="utf-8", check=check,
  43. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  44. output = res.stdout
  45. if self.debug:
  46. print(f'Result: {res.returncode}')
  47. print(f'---\n{output}\n---\n')
  48. return output, res.returncode
  49. def test_version(self):
  50. """test version information output"""
  51. output, _ = self.run_test(['--version'])
  52. self.assertIn('GNU General Public License', output)
  53. self.assertRegex(output, r'jpegoptim v\d+\.\d+\.\d+')
  54. def test_noarguments(self):
  55. """test running withouth arguments"""
  56. output, res = self.run_test([], check=False)
  57. self.assertEqual(1, res)
  58. self.assertRegex(output, r'file argument\(?s\)? missing')
  59. def test_default(self):
  60. """test default optimization"""
  61. output, _ = self.run_test(['jpegoptim_test1.jpg'], directory='tmp/default')
  62. self.assertTrue(os.path.exists('tmp/default/jpegoptim_test1.jpg'))
  63. self.assertRegex(output, r'\s\[OK\]\s.*\soptimized\.\s*$')
  64. # check that output file is indeed smaller than the input file
  65. self.assertGreater(os.path.getsize('jpegoptim_test1.jpg'),
  66. os.path.getsize('tmp/default/jpegoptim_test1.jpg'))
  67. # check that output file is valid and "optimized"
  68. output, _ = self.run_test(['-n', 'tmp/default/jpegoptim_test1.jpg'], check=False)
  69. self.assertRegex(output, r'\s\[OK\]\s.*\sskipped\.\s*$')
  70. def test_lossy(self):
  71. """test lossy optimization"""
  72. output, _ = self.run_test(['-m', '10', 'jpegoptim_test1.jpg'],
  73. directory='tmp/lossy')
  74. self.assertTrue(os.path.exists('tmp/lossy/jpegoptim_test1.jpg'))
  75. self.assertRegex(output, r'\s\[OK\]\s.*\soptimized\.\s*$')
  76. # check that output file is indeed smaller than the input file
  77. self.assertGreater(os.path.getsize('jpegoptim_test1.jpg'),
  78. os.path.getsize('tmp/lossy/jpegoptim_test1.jpg'))
  79. # check that output file is valid and "optimized"
  80. output, _ = self.run_test(['-n', 'tmp/lossy/jpegoptim_test1.jpg'], check=False)
  81. self.assertRegex(output, r'\s\[OK\]\s.*\sskipped\.\s*$')
  82. def test_optimized(self):
  83. """test already optimized image"""
  84. output, _ = self.run_test(['jpegoptim_test2.jpg'],
  85. directory='tmp/optimized')
  86. self.assertRegex(output, r'\s\[OK\]\s.*\sskipped\.\s*$')
  87. def test_broken(self):
  88. """test broken image"""
  89. output, _ = self.run_test(['jpegoptim_test2-broken.jpg'],
  90. directory='tmp/broken', check=False)
  91. self.assertRegex(output, r'\s\[WARNING\]\s.*\sskipped\.\s*$')
  92. if __name__ == '__main__':
  93. unittest.main()
  94. # eof :-)