7
7
8
8
from grading_lib import is_debug_mode
9
9
from grading_lib .common import (
10
+ BaseTestCase ,
10
11
get_mtime_as_datetime ,
11
12
get_seed_from_env ,
12
13
has_file_changed ,
14
+ populate_folder_with_filenames ,
15
+ run_executable ,
13
16
)
14
17
15
18
@@ -29,10 +32,23 @@ def test_is_debug_mode():
29
32
30
33
31
34
def test_get_seed_from_env ():
35
+ """
36
+ If value is set, should properly parse and use it.
37
+
38
+ If not, the value should be the scaled time (to avoid same seed
39
+ for multiple tests). Does not currently check if the return value is
40
+ the scaled time.
41
+ """
32
42
val = 123
33
43
os .environ ["test_SEED" ] = str (val )
34
44
assert get_seed_from_env ("test_SEED" ) == val
35
45
46
+ val = "123not-a-number"
47
+ os .environ ["test_SEED" ] = str (val )
48
+ res = get_seed_from_env ("test_SEED" )
49
+ assert isinstance (res , int )
50
+ assert res != 0
51
+
36
52
37
53
@pytest .fixture
38
54
def a_temp_file ():
@@ -45,9 +61,80 @@ def a_temp_file():
45
61
def test_has_file_changed (a_temp_file ):
46
62
path = Path (a_temp_file .name )
47
63
64
+ # Take snapshot of the mtime.
48
65
last_known_mtime = get_mtime_as_datetime (path )
49
66
assert not has_file_changed (last_known_mtime , path )
50
67
51
- time .sleep (2 )
68
+ # Modify the mtime and test.
69
+ time .sleep (1.2 )
52
70
path .touch ()
53
71
assert has_file_changed (last_known_mtime , path )
72
+
73
+ # Take another snapshot.
74
+ last_known_mtime = get_mtime_as_datetime (str (path ))
75
+ assert not has_file_changed (last_known_mtime , path )
76
+
77
+
78
+ def test_populate_folder_with_filenames ():
79
+ with tempfile .TemporaryDirectory () as tmpdir :
80
+ tmpdir_path = Path (tmpdir )
81
+
82
+ with pytest .raises (ValueError ):
83
+ (tmpdir_path / "a.txt" ).touch ()
84
+ populate_folder_with_filenames (tmpdir_path / "a.txt" , ["a" , "b" , "c" ])
85
+
86
+ with pytest .raises (ValueError ):
87
+ populate_folder_with_filenames (tmpdir_path / "src" , ["a" , "b" , "c" ])
88
+
89
+ with tempfile .TemporaryDirectory () as tmpdir :
90
+ tmpdir_path = Path (tmpdir )
91
+
92
+ expected_files = ["a" , "b" , "c" ]
93
+ populate_folder_with_filenames (tmpdir , expected_files )
94
+
95
+ result_names = [item .name for item in tmpdir_path .iterdir ()]
96
+
97
+ assert result_names == expected_files
98
+
99
+
100
+ def test_run_executable ():
101
+ cmd_result = run_executable (["git" , "version" ])
102
+ assert cmd_result .success
103
+ assert "git version" in cmd_result .output
104
+
105
+
106
+ def test_BaseTestCase ():
107
+ """Tests for the BaseTestCase."""
108
+
109
+ # When with_temporary_dir is not specified,
110
+ class ChildClsWithoutTempDir (BaseTestCase ):
111
+ pass
112
+
113
+ assert ChildClsWithoutTempDir .with_temporary_dir is False
114
+
115
+ # When with_temporary_dir is specified,
116
+ class ChildClsWithTempDir (BaseTestCase ):
117
+ with_temporary_dir = True
118
+
119
+ instance = ChildClsWithTempDir ()
120
+ instance .setUp ()
121
+ try :
122
+ assert instance .temporary_dir is not None
123
+ assert isinstance (instance .temporary_dir_path , Path )
124
+ finally :
125
+ instance .tearDown ()
126
+
127
+
128
+ def test_BaseTestCase_assertArchiveFileIsGzip ():
129
+ class ChildClsWithTempDir (BaseTestCase ):
130
+ with_temporary_dir = True
131
+
132
+ instance = ChildClsWithTempDir ()
133
+ instance .setUp ()
134
+
135
+ try :
136
+ with tempfile .TemporaryFile () as tmp_file :
137
+ with pytest .raises (AssertionError ):
138
+ instance .assertArchiveFileIsGzip (tmp_file )
139
+ finally :
140
+ instance .tearDown ()
0 commit comments