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