Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_guess_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
kind = filetype.guess(buf)
self.assertTrue(kind is not None)
self.assertEqual(kind.mime, 'image/jpeg')
self.assertEqual(kind.extension, 'jpg')
def test_guess_mime_file_path(self):
mime = filetype.guess_mime(FIXTURES + '/sample.jpg')
self.assertTrue(mime is not None)
self.assertEqual(mime, 'image/jpeg')
def test_guess_mime_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
mime = filetype.guess_mime(buf)
self.assertTrue(mime is not None)
self.assertEqual(mime, 'image/jpeg')
def test_guess_mime_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
mime = filetype.guess_mime(buf)
self.assertTrue(mime is not None)
self.assertEqual(mime, 'image/jpeg')
def test_guess_mime_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
mime = filetype.guess_mime(buf)
self.assertTrue(mime is None)
def test_guess_extension_buffer_invalid(self):
buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00])
ext = filetype.guess_extension(buf)
self.assertTrue(ext is None)
def test_guess_extension_file_path(self):
ext = filetype.guess_extension(FIXTURES + '/sample.jpg')
self.assertTrue(ext is not None)
self.assertEqual(ext, 'jpg')
def test_guess_extension_buffer(self):
buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
ext = filetype.guess_extension(buf)
self.assertTrue(ext is not None)
self.assertEqual(ext, 'jpg')
def test_guess_extension_memoryview(self):
buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08]))
ext = filetype.guess_extension(buf)
self.assertTrue(ext is not None)
self.assertEqual(ext, 'jpg')
def __call__(self, parser, namespace, values, option_string=None):
# Check if the given path is a file
if not os.path.isfile(values):
print('ERROR: {} is not a file.'.format(values))
sys.exit(1)
# Check if the given file is a native terraform plan file
given_file = filetype.guess(values)
if given_file is not None:
terraform_executable = getattr(namespace, 'terraform_file', None)
values = convert_terraform_plan_to_json(os.path.abspath(values), terraform_executable)
# Check if the given file is a json file.
try:
with open(values, 'r', encoding='utf-8') as plan_file:
data = json.load(plan_file)
except json.decoder.JSONDecodeError:
print('ERROR: {} is not a valid JSON file'.format(values))
sys.exit(1)
except UnicodeDecodeError:
print('ERROR: {} is not a valid JSON file.'.format(values))
print(' Did you try to convert the binary plan file to json with '