Skip to content

Commit 69cd760

Browse files
authored
Merge pull request #494 from yuvipanda/util-tests
Add unit tests for repo2docker/utils.py
2 parents 694e728 + d934c59 commit 69cd760

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed

repo2docker/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import os
1515
import pwd
1616
import subprocess
17+
import shutil
1718
import tempfile
1819
import time
1920

@@ -35,7 +36,7 @@
3536
)
3637
from . import contentproviders
3738
from .utils import (
38-
ByteSpecification, maybe_cleanup, is_valid_docker_image_name,
39+
ByteSpecification, is_valid_docker_image_name,
3940
validate_and_generate_port_mapping, chdir
4041
)
4142

@@ -723,9 +724,7 @@ def start(self):
723724
else:
724725
checkout_path = self.git_workdir
725726

726-
# keep as much as possible in the context manager to make sure we
727-
# cleanup if things go wrong
728-
with maybe_cleanup(checkout_path, self.cleanup_checkout):
727+
try:
729728
self.fetch(self.repo, self.ref, checkout_path)
730729

731730
if self.subdir:
@@ -771,6 +770,10 @@ def start(self):
771770
else:
772771
self.log.info(json.dumps(l),
773772
extra=dict(phase='building'))
773+
finally:
774+
# Cheanup checkout if necessary
775+
if self.cleanup_checkout:
776+
shutil.rmtree(checkout_path, ignore_errors=True)
774777

775778
if self.push:
776779
self.push_image()

repo2docker/utils.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
from functools import partial
33
import os
44
import re
5-
import shutil
65
import subprocess
76

87
from traitlets import Integer, TraitError
98

109

1110
def execute_cmd(cmd, capture=False, **kwargs):
1211
"""
13-
Call given command, yielding output line by line if capture=True
12+
Call given command, yielding output line by line if capture=True.
13+
14+
Must be yielded from.
1415
"""
1516
if capture:
1617
kwargs['stdout'] = subprocess.PIPE
@@ -67,14 +68,6 @@ def chdir(path):
6768
os.chdir(old_dir)
6869

6970

70-
@contextmanager
71-
def maybe_cleanup(path, cleanup=False):
72-
"""Delete the directory at passed path if cleanup flag is True."""
73-
yield
74-
if cleanup:
75-
shutil.rmtree(path, ignore_errors=True)
76-
77-
7871
def validate_and_generate_port_mapping(port_mapping):
7972
"""
8073
Validate the port mapping list and return a list of validated tuples.

tests/test_utils.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Tests for repo2docker/utils.py
3+
"""
4+
import traitlets
5+
import os
6+
from tempfile import TemporaryDirectory
7+
from repo2docker import utils
8+
import pytest
9+
import subprocess
10+
11+
12+
def test_capture_cmd_no_capture_success():
13+
# This should succeed
14+
for line in utils.execute_cmd([
15+
'/bin/bash', '-c', 'echo test'
16+
]):
17+
pass
18+
19+
def test_capture_cmd_no_capture_fail():
20+
with pytest.raises(subprocess.CalledProcessError):
21+
for line in utils.execute_cmd([
22+
'/bin/bash', '-c', 'e '
23+
]):
24+
pass
25+
26+
27+
def test_capture_cmd_capture_success():
28+
# This should succeed
29+
for line in utils.execute_cmd([
30+
'/bin/bash', '-c', 'echo test'
31+
], capture=True):
32+
assert line == 'test\n'
33+
34+
35+
def test_capture_cmd_capture_fail():
36+
with pytest.raises(subprocess.CalledProcessError):
37+
for line in utils.execute_cmd([
38+
'/bin/bash', '-c', 'echo test; exit 1 '
39+
], capture=True):
40+
assert line == 'test\n'
41+
42+
43+
def test_chdir():
44+
with TemporaryDirectory() as d:
45+
cur_cwd = os.getcwd()
46+
with utils.chdir(d):
47+
assert os.getcwd() == d
48+
assert os.getcwd() == cur_cwd
49+
50+
51+
def test_byte_spec_validation():
52+
bs = utils.ByteSpecification()
53+
54+
assert bs.validate(None, 1) == 1
55+
assert bs.validate(None, 1.0) == 1.0
56+
57+
assert bs.validate(None, '1K') == 1024
58+
assert bs.validate(None, '1M') == 1024 * 1024
59+
assert bs.validate(None, '1G') == 1024 * 1024 * 1024
60+
assert bs.validate(None, '1T') == 1024 * 1024 * 1024 * 1024
61+
62+
with pytest.raises(traitlets.TraitError):
63+
bs.validate(None, 'NK')
64+
65+
with pytest.raises(traitlets.TraitError):
66+
bs.validate(None, '1m')

0 commit comments

Comments
 (0)