From 6466c9953fc5e5e1cf6b483609ca586288a95191 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Sun, 13 Oct 2019 13:00:53 -0700 Subject: [PATCH] Add test for process command and input path inspection --- .../test/test_process_command.py | 90 +++++++++++++++++++ .../tracetools_analysis/process.py | 1 + 2 files changed, 91 insertions(+) create mode 100644 tracetools_analysis/test/test_process_command.py diff --git a/tracetools_analysis/test/test_process_command.py b/tracetools_analysis/test/test_process_command.py new file mode 100644 index 0000000..141039f --- /dev/null +++ b/tracetools_analysis/test/test_process_command.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +# Copyright 2019 Apex.AI, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import shutil +import tempfile +import unittest + +from tracetools_analysis.process import inspect_input_path + + +class TestProcessCommand(unittest.TestCase): + + def __init__(self, *args) -> None: + super().__init__( + *args, + ) + + def setUp(self): + self.test_dir_path = tempfile.mkdtemp() + + # Create directory that contains a 'converted' file + self.with_converted_file_dir = os.path.join( + self.test_dir_path, + 'with_converted_file', + ) + os.mkdir(self.with_converted_file_dir) + self.converted_file_path = os.path.join( + self.with_converted_file_dir, + 'converted', + ) + open(self.converted_file_path, 'a').close() + self.assertTrue(os.path.exists(self.converted_file_path)) + + # Create directory that contains a file with another name that is not 'converted' + self.without_converted_file_dir = os.path.join( + self.test_dir_path, + 'without_converted_file', + ) + os.mkdir(self.without_converted_file_dir) + self.random_file_path = os.path.join( + self.without_converted_file_dir, + 'a_file', + ) + open(self.random_file_path, 'a').close() + self.assertTrue(os.path.exists(self.random_file_path)) + + def tearDown(self): + shutil.rmtree(self.test_dir_path) + + def test_inspect_input_path(self) -> None: + # Should find converted file under directory + file_path, create_file = inspect_input_path(self.with_converted_file_dir, False) + self.assertEqual(self.converted_file_path, file_path) + self.assertFalse(create_file) + # Should find it but set it to be re-created + file_path, create_file = inspect_input_path(self.with_converted_file_dir, True) + self.assertEqual(self.converted_file_path, file_path) + self.assertTrue(create_file) + + # Should fail to find converted file under directory + file_path, create_file = inspect_input_path(self.without_converted_file_dir, False) + self.assertIsNone(file_path) + self.assertIsNone(create_file) + file_path, create_file = inspect_input_path(self.without_converted_file_dir, True) + self.assertIsNone(file_path) + self.assertIsNone(create_file) + + # Should accept any file path if it exists + file_path, create_file = inspect_input_path(self.random_file_path, False) + self.assertEqual(self.random_file_path, file_path) + self.assertFalse(create_file) + # Should set it to be re-created + file_path, create_file = inspect_input_path(self.random_file_path, True) + self.assertEqual(self.random_file_path, file_path) + self.assertTrue(create_file) + + # TODO try with a trace directory diff --git a/tracetools_analysis/tracetools_analysis/process.py b/tracetools_analysis/tracetools_analysis/process.py index a199f4c..90fb615 100644 --- a/tracetools_analysis/tracetools_analysis/process.py +++ b/tracetools_analysis/tracetools_analysis/process.py @@ -99,6 +99,7 @@ def inspect_input_path( return converted_file_path, True else: # Simplest use-case: given path is an existing converted file + print(f'found converted file: {converted_file_path}') return converted_file_path, False