Skip to content

Commit

Permalink
always use absolute path for working dir because otherwise some comma…
Browse files Browse the repository at this point in the history
…nds may end up changing what the relative path is relative too, looking at you maven.
  • Loading branch information
itewk committed Jul 21, 2021
1 parent 18541de commit 99bcd28
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/ploigos_step_runner/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions src/ploigos_step_runner/utils/maven.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Shared utils for maven operations.
"""

import os
import xml.etree.ElementTree as ET

import sh
Expand Down Expand Up @@ -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',
Expand Down
33 changes: 31 additions & 2 deletions tests/utils/test_maven.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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')

Expand All @@ -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}'
)

0 comments on commit 99bcd28

Please sign in to comment.