diff --git a/tests/test_cli.py b/tests/test_cli.py index d64854f..39d427b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -7,6 +7,10 @@ from yaclog.cli.__main__ import cli from click.testing import CliRunner +def check_result(runner, result, expected=0): + runner.assertEqual(result.exit_code, expected, f'output: {result.output}\ntraceback: {result.exc_info}') + + class TestCreation(unittest.TestCase): def test_init(self): """Test creating and overwriting a changelog""" @@ -16,7 +20,7 @@ class TestCreation(unittest.TestCase): with runner.isolated_filesystem(): result = runner.invoke(cli, ['init']) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) self.assertTrue(os.path.exists(os.path.abspath(location)), 'yaclog init did not create a file') self.assertIn(location, result.output, "yaclog init did not echo the file's correct location") @@ -24,7 +28,7 @@ class TestCreation(unittest.TestCase): fp.write(err_str) result = runner.invoke(cli, ['init'], input='y\n') - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) self.assertTrue(os.path.exists(os.path.abspath(location)), 'file no longer exists after overwrite') self.assertIn(location, result.output, "yaclog init did not echo the file's correct location") @@ -38,7 +42,7 @@ class TestCreation(unittest.TestCase): with runner.isolated_filesystem(): result = runner.invoke(cli, ['--path', location, 'init']) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) self.assertTrue(os.path.exists(os.path.abspath(location)), 'yaclog init did not create a file') self.assertIn(location, result.output, "yaclog init did not echo the file's correct location") @@ -48,7 +52,7 @@ class TestCreation(unittest.TestCase): with runner.isolated_filesystem(): result = runner.invoke(cli, ['show']) - self.assertNotEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result, 1) self.assertIn('does not exist', result.output) @@ -66,17 +70,17 @@ class TestTagging(unittest.TestCase): in_log.write() result = runner.invoke(cli, ['tag', 'tag1']) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) result = runner.invoke(cli, ['tag', 'tag2', '0.9.0']) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) out_log = yaclog.read(location) self.assertEqual(out_log.versions[0].tags, ['TAG1']) self.assertEqual(out_log.versions[1].tags, ['TAG2']) result = runner.invoke(cli, ['tag', 'tag3', '0.8.0']) - self.assertNotEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result, 2) self.assertIn('not found in changelog', result.output) def test_tag_deletion(self): @@ -96,16 +100,16 @@ class TestTagging(unittest.TestCase): in_log.write() result = runner.invoke(cli, ['tag', '-d', 'tag2', '0.8.0']) - self.assertNotEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result, 2) self.assertIn('not found in changelog', result.output) result = runner.invoke(cli, ['tag', '-d', 'tag3', '0.9.0']) - self.assertNotEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result, 2) self.assertIn('not found in version', result.output) result = runner.invoke(cli, ['tag', '-d', 'tag1']) self.assertNotIn('not found in version', result.output) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) out_log = yaclog.read(location) self.assertEqual(out_log.versions[0].tags, []) @@ -113,7 +117,7 @@ class TestTagging(unittest.TestCase): result = runner.invoke(cli, ['tag', '-d', 'tag2', '0.9.0']) self.assertNotIn('not found in version', result.output) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) out_log = yaclog.read(location) self.assertEqual(out_log.versions[0].tags, []) @@ -130,7 +134,7 @@ class TestRelease(unittest.TestCase): runner.invoke(cli, ['entry', '-b', 'entry number 1']) result = runner.invoke(cli, ['release', '--version', '1.0.0']) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) self.assertEqual(yaclog.read(location).versions[0].name, '1.0.0') self.assertIn('Unreleased', result.output) self.assertIn('1.0.0', result.output) @@ -138,7 +142,7 @@ class TestRelease(unittest.TestCase): runner.invoke(cli, ['entry', '-b', 'entry number 2']) result = runner.invoke(cli, ['release', '-p']) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) self.assertEqual(yaclog.read(location).versions[0].name, '1.0.1') self.assertIn('Unreleased', result.output) self.assertIn('1.0.1', result.output) @@ -146,7 +150,7 @@ class TestRelease(unittest.TestCase): runner.invoke(cli, ['entry', '-b', 'entry number 3']) result = runner.invoke(cli, ['release', '-m']) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) self.assertEqual(yaclog.read(location).versions[0].name, '1.1.0') self.assertIn('Unreleased', result.output) self.assertIn('1.1.0', result.output) @@ -154,7 +158,7 @@ class TestRelease(unittest.TestCase): runner.invoke(cli, ['entry', '-b', 'entry number 4']) result = runner.invoke(cli, ['release', '-M']) - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) self.assertEqual(yaclog.read(location).versions[0].name, '2.0.0') self.assertIn('Unreleased', result.output) self.assertIn('2.0.0', result.output) @@ -167,16 +171,21 @@ class TestRelease(unittest.TestCase): os.chdir('testing') repo.index.commit('initial commit') + with repo.config_writer() as cw: + cw.set_value('user', 'email', 'unit-tester@example.com') + cw.set_value('user', 'name', 'unit-tester') + runner.invoke(cli, ['init']) # create the changelog runner.invoke(cli, ['entry', '-b', 'entry number 1']) result = runner.invoke(cli, ['release', '--version', '1.0.0', '-c'], input='y\n') - self.assertEqual(result.exit_code, 0, f'output: {result.output}\ntraceback: {result.exc_info}') + check_result(self, result) self.assertIn('Created commit', result.output) self.assertIn('Created tag', result.output) self.assertIn(repo.head.commit.hexsha[0:7], result.output) self.assertEqual(repo.tags[0].name, '1.0.0') + if __name__ == '__main__': unittest.main()