Skip to content

Instantly share code, notes, and snippets.

@schwehr
Created October 13, 2023 17:35
Show Gist options
  • Save schwehr/163029772e423848fa1c4769b0a8eecf to your computer and use it in GitHub Desktop.
Save schwehr/163029772e423848fa1c4769b0a8eecf to your computer and use it in GitHub Desktop.
jpeginfo: Possible starter test
#!/usr/bin/env python3
# Copyright 2023 Google Inc. All Rights Reserved.
"""Tests for jpeginfo command line app."""
import json
import subprocess
import unittest
class JpegInfoTest(unittest.TestCase):
def setUp(self):
super().setUp()
self.command = './jpeginfo'
def testHelp(self):
command = [self.command, '--help']
output = subprocess.check_output(
command, stderr=subprocess.STDOUT).decode("utf-8")
self.assertIn('jpeginfo v', output)
self.assertIn('Usage: jpeginfo [options] <filenames>', output)
self.assertIn('-d, --delete Delete files that have errors', output)
self.assertIn('Mode can be one of the following:', output)
def testVersion(self):
command = [self.command, '--version']
output = subprocess.check_output(command).decode("utf-8")
self.assertIn('jpeginfo v', output)
self.assertIn('Copyright', output)
self.assertIn('GNU General Public License', output)
self.assertIn('libjpeg version:', output)
def testNoArgs(self):
command = [self.command]
raised = False
try:
subprocess.check_output(command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raised = True
self.assertEqual(1, e.returncode)
output = e.output.decode('utf-8')
self.assertIn('jpeginfo: file arguments missing', output)
self.assertTrue(raised)
def test_simple(self):
command = [self.command, 'testdata/test1.jpg']
output = subprocess.check_output(command).decode("utf-8").rstrip()
self.assertRegex(
output, r'testdata/test1.jpg +100 x +44 24bit P ICC,Adobe +6095')
def test_simple_not_a_jpeg(self):
command = [self.command, 'README']
raised = False
try:
subprocess.check_output(command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raised = True
self.assertEqual(1, e.returncode)
output = e.output.decode('utf-8')
self.assertRegex(
output,
r'README + 0 x +0 +0bit N +[0-9]+ +ERROR +'
r'Not a JPEG file: starts with 0x')
self.assertTrue(raised)
def test_md5(self):
command = [self.command, '--md5', 'testdata/test2.jpg']
output = subprocess.check_output(command).decode("utf-8").rstrip()
self.assertEqual(
'329f135d82dfece6127117cfdfff2bea', output.split(' ')[-1])
def test_sha256(self):
command = [self.command, '--sha256', 'testdata/test2.jpg']
output = subprocess.check_output(command).decode("utf-8").rstrip()
self.assertEqual(
'6f220bd7107faab32dbc4a991ef8aed4b2a68d967a84e93dfb5c0408766f8828',
output.split(' ')[-1])
def test_sha512(self):
command = [self.command, '--sha512', 'testdata/test2.jpg']
output = subprocess.check_output(command).decode("utf-8").rstrip()
self.assertEqual(
'b8e88cfb2fdcc6291d8671fe811046954551f3db29e846c59d9d68bbd3f16330e2'
'1202157726c9b5a08b6e3ccaec16fafcb6ed75042c8c5dbc9bbeab20230b2e',
output.split(' ')[-1])
def test_check(self):
command = [self.command, '--check', 'testdata/test1.jpg']
output = subprocess.check_output(command).decode("utf-8").rstrip()
self.assertRegex(
output, r'testdata/test1.jpg +100 x +44 24bit P ICC,Adobe +6095 OK')
def test_comment(self):
command = [self.command, '--comment', 'testdata/test6-comment.jpg']
output = subprocess.check_output(command).decode("utf-8")
self.assertRegex(
output,
r'testdata/test6-comment.jpg +100 +x +44 +8bit +N +JFIF,ICC,COM +4052 +'
r'A comment string.')
def test_header(self):
command = [self.command, '--header', 'testdata/test1.jpg']
output = subprocess.check_output(command).decode("utf-8")
self.assertRegex(
output, r'Filename +W +x +H +Color +P +Markers +Size')
def test_info(self):
command = [self.command, '--info', 'testdata/test1.jpg']
output = subprocess.check_output(command).decode("utf-8")
self.assertIn(' Huffman ', output)
def test_info_with_header(self):
command = [self.command, '--header', '--info', 'testdata/test1.jpg']
output = subprocess.check_output(command).decode("utf-8")
self.assertIn(' ExtraInfo ', output)
def test_csv(self):
command = [self.command, '--csv', 'testdata/test3-cmyk.jpg']
output = subprocess.check_output(command).decode("utf-8").rstrip()
expect = (
'"testdata/test3-cmyk.jpg",6085,"",100,44,"24bit","ICC,Adobe","P",'
'"Huffman","","",""')
self.assertEqual(expect, output)
def test_csv_header(self):
command = [self.command, '--csv', '--header', 'testdata/test3-cmyk.jpg']
output = subprocess.check_output(command).decode("utf-8")
expect = (
'filename,size,hash,width,height,color_depth,markers,'
'progressive_normal,extra_info,comments,status,status_detail')
self.assertIn(expect, output)
def test_verbose(self):
command = [self.command, '--verbose', 'testdata/test1.jpg']
output = subprocess.check_output(
command, stderr=subprocess.STDOUT).decode("utf-8")
self.assertIn('Reading file: testdata/test1.jpg', output)
self.assertIn('Found marker APP2 (0xE2)', output)
self.assertIn('Found marker APP14 (0xEE)', output)
self.assertIn('Found total of 2 markers', output)
self.assertIn('0 unknown markers', output)
def test_json(self):
command = [self.command, '--json', 'testdata/test1.jpg', 'testdata/test5-grayscale.jpg']
output = subprocess.check_output(command).decode("utf-8")
result = json.loads(output)
expect = [
{
'filename': 'testdata/test1.jpg',
'size': 6095,
'hash': '',
'width': 100,
'height': 44,
'color_depth': '24bit',
'type': 'ICC,Adobe',
'mode': 'Progressive',
'info': 'Huffman',
'comments': '',
'status': '',
'status_detail': ''
}, {
'filename': 'testdata/test5-grayscale.jpg',
'size': 4036,
'hash': '',
'width': 100,
'height': 44,
'color_depth': '8bit',
'type': 'JFIF,ICC',
'mode': 'Normal',
'info': 'Huffman',
'comments': '',
'status': '',
'status_detail': ''
}
]
self.assertEqual(expect, result)
# https://github.com/OSGeo/gdal/tree/2cfeb1a9b03716c8348d6f1f4c611f2589d53e76/autotest/gdrivers/data/jpeg/albania.jpg
def test_24bit_exif(self):
command = [self.command, '-j', 'testdata/albania.jpg']
output = subprocess.check_output(command).decode("utf-8")
result = json.loads(output)
expect = [{
'filename': 'testdata/albania.jpg',
'size': 12574,
'hash': '',
'width': 361,
'height': 260,
'color_depth': '24bit',
'type': 'JFIF,Exif',
'mode': 'Normal',
'info': 'Huffman',
'comments': '',
'status': '',
'status_detail': ''
}]
self.assertEqual(expect, result)
# https://github.com/OSGeo/gdal/tree/2cfeb1a9b03716c8348d6f1f4c611f2589d53e76/autotest/gdrivers/data/jpeg/rgb_ntf_cmyk.jpg
def test_32bit_and_dpi(self):
command = [self.command, '-j', 'testdata/rgb_ntf_cmyk.jpg']
output = subprocess.check_output(command).decode("utf-8")
result = json.loads(output)
expect = [{
'filename': 'testdata/rgb_ntf_cmyk.jpg',
'size': 9024,
'hash': '',
'width': 50,
'height': 50,
'color_depth': '32bit',
'type': 'JFIF,Adobe',
'mode': 'Normal',
'info': 'Huffman,72dpi',
'comments': '',
'status': '',
'status_detail': ''
}]
self.assertEqual(expect, result)
# TODO(schwehr): Add test for --delete
# TODO(schwehr): Add test for --files-from
# TODO(schwehr): Add test for --files-stdin
# TODO(schwehr): Add test for --lsstyle
# TODO(schwehr): Add test for --mode
# TODO(schwehr): Add test for --quiet
# TODO(schwehr): Add test for --stdin
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment