From 99bcd28458e3abd7a0f2347cb1bcd8e397cae10e Mon Sep 17 00:00:00 2001 From: Ian Tewksbury Date: Wed, 21 Jul 2021 15:13:43 -0400 Subject: [PATCH] always use absolute path for working dir because otherwise some commands may end up changing what the relative path is relative too, looking at you maven. --- src/ploigos_step_runner/__main__.py | 7 +++++- src/ploigos_step_runner/utils/maven.py | 10 ++++++++ tests/utils/test_maven.py | 33 ++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/ploigos_step_runner/__main__.py b/src/ploigos_step_runner/__main__.py index 85518f35b..2fe78af77 100644 --- a/src/ploigos_step_runner/__main__.py +++ b/src/ploigos_step_runner/__main__.py @@ -111,7 +111,12 @@ def main(argv=None): sys.exit(102) config.set_step_config_overrides(args.step, args.step_config) - step_runner = StepRunner(config) + # it is VERY important that the working dir be an absolute path because some + # commands (looking at you maven) will change the context of relative paths on you + step_runner = StepRunner( + config=config, + work_dir_path=os.path.abspath('step-runner-working') + ) try: if not step_runner.run_step(args.step, args.environment): diff --git a/src/ploigos_step_runner/utils/maven.py b/src/ploigos_step_runner/utils/maven.py index ceeb3d948..490c338d9 100644 --- a/src/ploigos_step_runner/utils/maven.py +++ b/src/ploigos_step_runner/utils/maven.py @@ -1,6 +1,7 @@ """Shared utils for maven operations. """ +import os import xml.etree.ElementTree as ET import sh @@ -420,6 +421,15 @@ def write_effective_pom( If issue generating effective pom. """ + if not os.path.isabs(output_path): + raise StepRunnerException( + f"Given output path ({output_path}) is not absolute which will mean your output" + f" file will actually end up being relative to the pom file ({pom_file_path}) rather" + " than your expected root. Rather then handling this, just give this function an" + " absolute path." + " If you are a user seeing this, a programmer messed up somewhere, report an issue." + ) + try: sh.mvn( # pylint: disable=no-member 'help:effective-pom', diff --git a/tests/utils/test_maven.py b/tests/utils/test_maven.py index 5c90362ec..4f0316109 100644 --- a/tests/utils/test_maven.py +++ b/tests/utils/test_maven.py @@ -971,7 +971,7 @@ def test_add_maven_mirrors_dict_missing_mirror_of(self): @patch('sh.mvn', create=True) def test_write_effective_pom_success(self, mvn_mock): pom_file_path = 'input/pom.xml' - effective_pom_path = 'output/effective-pom.xml' + effective_pom_path = '/tmp/output/effective-pom.xml' actual_effective_pom_path = write_effective_pom( pom_file_path=pom_file_path, @@ -987,7 +987,7 @@ def test_write_effective_pom_success(self, mvn_mock): @patch('sh.mvn', create=True) def test_write_effective_pom_fail(self, mvn_mock): pom_file_path = 'input/pom.xml' - effective_pom_path = 'output/effective-pom.xml' + effective_pom_path = '/tmp/output/effective-pom.xml' mvn_mock.side_effect = sh.ErrorReturnCode('mvn', b'mock stdout', b'mock error') @@ -1012,3 +1012,32 @@ def test_write_effective_pom_fail(self, mvn_mock): f'-f={pom_file_path}', f'-Doutput={effective_pom_path}' ) + + @patch('sh.mvn', create=True) + def test_write_effective_pom_fail_not_absolute_path(self, mvn_mock): + pom_file_path = 'input/pom.xml' + effective_pom_path = 'output/effective-pom.xml' + + mvn_mock.side_effect = sh.ErrorReturnCode('mvn', b'mock stdout', b'mock error') + + with self.assertRaisesRegex( + StepRunnerException, + re.compile( + rf"Given output path \({effective_pom_path}\) is not absolute which will mean " + rf"your output file will actually end up being relative to the pom file " + rf"\({pom_file_path}\) rather than your expected root. Rather then handling this, " + rf"just give this function an absolute path. " + rf"If you are a user seeing this, a programmer messed up somewhere, " + rf"report an issue.", + re.DOTALL + ) + ): + write_effective_pom( + pom_file_path=pom_file_path, + output_path=effective_pom_path + ) + mvn_mock.assert_any_call( + 'help:effective-pom', + f'-f={pom_file_path}', + f'-Doutput={effective_pom_path}' + ) \ No newline at end of file