def test_utilio_execution(self): result = execute(None, quiet=True, critical=False) self.assertFalse(result) result = execute([], quiet=True, critical=False) self.assertFalse(result) test_cmd = ['python', '-c', 'print("Hello")'] result = execute(test_cmd, quiet=True, critical=False) self.assertTrue(result) result = execute(['an_unknown_command'], quiet=True, critical=False) self.assertFalse(result) # skip output checks if verbose mode is enabled if is_verbose(): raise unittest.SkipTest( 'ignoring execution output checks while in verbose mode') # verify output with redirect_stdout() as stream: test_cmd = [sys.executable, '-c', 'print("Hello")'] result = execute(test_cmd, critical=False) self.assertTrue(result) self.assertEqual(stream.getvalue().strip(), 'Hello') # verify quiet mode with redirect_stdout() as stream: test_cmd = [sys.executable, '-c', 'print("Hello")'] result = execute(test_cmd, quiet=True, critical=False) self.assertTrue(result) self.assertEqual(stream.getvalue().strip(), '') # verify capture mode which will be silent out = [] with redirect_stdout() as stream: test_cmd = [sys.executable, '-c', 'print("Hello")'] result = execute(test_cmd, critical=False, capture=out) self.assertTrue(result) self.assertEqual(''.join(out), 'Hello') self.assertEqual(stream.getvalue().strip(), '') # verify capture mode that is also verbose out = [] with redirect_stdout() as stream: test_cmd = [sys.executable, '-c', 'print("Hello")'] result = execute(test_cmd, quiet=False, critical=False, capture=out) self.assertTrue(result) self.assertEqual(''.join(out), 'Hello') self.assertEqual(stream.getvalue().strip(), 'Hello')
def test_utilio_ls_valid_directory_empty(self): with redirect_stdout() as stream: with prepare_workdir() as empty_dir: listed = ls(empty_dir) self.assertTrue(listed) self.assertEqual(stream.getvalue(), '')
def test_utilio_cat_invalid_file(self): with redirect_stdout() as stream: with prepare_workdir() as test_dir: output = cat(test_dir) self.assertFalse(output) self.assertEqual(stream.getvalue(), '')
def test_utilio_cat_missing_file(self): with redirect_stdout() as stream: with prepare_workdir() as test_dir: test_file = os.path.join(test_dir, 'missing-file') output = cat(test_file) self.assertFalse(output) self.assertEqual(stream.getvalue(), '')
def test_utilio_ls_valid_directory_contents(self): base_dir = os.path.dirname(os.path.realpath(__file__)) with redirect_stdout() as stream: listed = ls(base_dir) self.assertTrue(listed) entries = stream.getvalue().split('\n') self.assertTrue('assets/' in entries) self.assertTrue('__init__.py' in entries) self.assertTrue('test_util_io_cat.py' in entries)
def test_utilio_cat_valid_empty(self): with redirect_stdout() as stream: with prepare_workdir() as test_dir: test_file = os.path.join(test_dir, 'test') with open(test_file, 'w'): pass # empty output = cat(test_file) self.assertTrue(output) self.assertEqual(stream.getvalue(), '')
def test_utilio_cat_valid_contents_single(self): with redirect_stdout() as stream: with prepare_workdir() as test_dir: test_file = os.path.join(test_dir, 'test') with open(test_file, 'w') as f: f.write('this is a test') output = cat(test_file) self.assertTrue(output) self.assertEqual(stream.getvalue(), 'this is a test')
def test_utilio_cat_valid_contents_multiple(self): with redirect_stdout() as stream: with prepare_workdir() as test_dir: test_file1 = os.path.join(test_dir, 'test1') with open(test_file1, 'w') as f: f.write('1') test_file2 = os.path.join(test_dir, 'test2') with open(test_file2, 'w') as f: f.write('2') test_file3 = os.path.join(test_dir, 'test3') with open(test_file3, 'w') as f: f.write('3') output = cat(test_file1, test_file2, test_file3) self.assertTrue(output) self.assertEqual(stream.getvalue(), '123')
def test_utilio_ls_missing_directory(self): with redirect_stdout() as stream: listed = ls('missing-directory') self.assertFalse(listed) self.assertEqual(stream.getvalue(), '')
def test_utilio_ls_invalid_directory(self): with redirect_stdout() as stream: listed = ls(os.path.realpath(__file__)) self.assertFalse(listed) self.assertEqual(stream.getvalue(), '')