Skip to content

Commit effba6f

Browse files
committed
refactor(load-task): build docker command in the logical order
1 parent 30dd32e commit effba6f

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/taskgraph/docker.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -540,24 +540,15 @@ def load_task(
540540
if isatty:
541541
command.append("-t")
542542

543-
command.append(image_tag)
544-
545-
if task_command:
546-
command.extend(task_command)
547-
548-
if volumes:
549-
for k, v in volumes.items():
550-
command[2:2] = ["-v", f"{k}:{v}"]
551-
552543
if remove:
553-
command.insert(2, "--rm")
544+
command.append("--rm")
554545

555546
if env:
556547
envfile = tempfile.NamedTemporaryFile("w+", delete=False)
557548
envfile.write("\n".join([f"{k}={v}" for k, v in env.items()]))
558549
envfile.close()
559550

560-
command.insert(2, f"--env-file={envfile.name}")
551+
command.append(f"--env-file={envfile.name}")
561552

562553
if exec_command:
563554
initfile = tempfile.NamedTemporaryFile("w+", delete=False)
@@ -576,7 +567,16 @@ def load_task(
576567
)
577568
initfile.close()
578569

579-
command[2:2] = ["-v", f"{initfile.name}:/builds/worker/.bashrc"]
570+
command.extend(["-v", f"{initfile.name}:/builds/worker/.bashrc"])
571+
572+
if volumes:
573+
for k, v in volumes.items():
574+
command.extend(["-v", f"{k}:{v}"])
575+
576+
command.append(image_tag)
577+
578+
if task_command:
579+
command.extend(task_command)
580580

581581
logger.info(f"Running: {' '.join(command)}")
582582
proc = subprocess.run(command)

test/test_docker.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ def test_load_task(run_load_task):
136136
expected = [
137137
"docker",
138138
"run",
139-
"-v",
140-
re.compile(f"{tempfile.gettempdir()}/tmp.*:/builds/worker/.bashrc"),
139+
"-i",
140+
"-t",
141141
re.compile(f"--env-file={tempfile.gettempdir()}/tmp.*"),
142142
"-v",
143-
"/another/host:/another/container",
143+
re.compile(f"{tempfile.gettempdir()}/tmp.*:/builds/worker/.bashrc"),
144144
"-v",
145145
"/host/path:/container/path",
146-
"-i",
147-
"-t",
146+
"-v",
147+
"/another/host:/another/container",
148148
"image/tag",
149149
"bash",
150150
"-c",
@@ -239,9 +239,9 @@ def test_load_task_env_init_and_remove(mocker, run_load_task):
239239
# Verify subprocess was called with the correct env file and init file
240240
mocks["subprocess_run"].assert_called_once()
241241
actual = mocks["subprocess_run"].call_args[0][0]
242-
assert actual[3] == "/tmp/test_initfile:/builds/worker/.bashrc"
243-
assert actual[4] == "--env-file=/tmp/test_envfile"
244-
assert actual[5] == "--rm"
242+
assert actual[4] == "--rm"
243+
assert actual[5] == "--env-file=/tmp/test_envfile"
244+
assert actual[6:8] == ["-v", "/tmp/test_initfile:/builds/worker/.bashrc"]
245245

246246

247247
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)