|
23 | 23 | checkFileString, |
24 | 24 | isSpring, |
25 | 25 | replaceFileString, |
| 26 | + _replaceFileStringInCommand, |
| 27 | + _replaceFileStringInSpring, |
26 | 28 | ) |
27 | 29 | from unittest import ( |
28 | 30 | main, |
@@ -62,31 +64,104 @@ def testCheckFileString(self): |
62 | 64 | self.assertFalse(checkFileString(command)) |
63 | 65 |
|
64 | 66 |
|
65 | | - def testReplaceFileString(self): |
66 | | - """Verify that the replaceFileString function works as expected.""" |
67 | | - self.assertFalse(replaceFileString(["cat"], ["test"]), None) |
68 | | - self.assertFalse(replaceFileString(["cat", "-o"], ["test"]), None) |
| 67 | + def testReplaceFileStringInCommand(self): |
| 68 | + """Verify that the _replaceFileStringInCommand function works as expected.""" |
| 69 | + self.assertFalse(_replaceFileStringInCommand(["cat"], ["test"]), None) |
| 70 | + self.assertFalse(_replaceFileStringInCommand(["cat", "-o"], ["test"]), None) |
69 | 71 |
|
70 | 72 | command = ["cat", "{file}"] |
71 | 73 | expected = ["cat", "test"] |
72 | | - self.assertTrue(replaceFileString(command, ["test"])) |
| 74 | + self.assertTrue(_replaceFileStringInCommand(command, ["test"])) |
73 | 75 | self.assertEqual(command, expected) |
74 | 76 |
|
75 | 77 | command = ["cat", "-o", "{file}", "-a", "test2"] |
76 | 78 | expected = ["cat", "-o", "test", "-a", "test2"] |
77 | | - self.assertTrue(replaceFileString(command, ["test"])) |
| 79 | + self.assertTrue(_replaceFileStringInCommand(command, ["test"])) |
78 | 80 | self.assertEqual(command, expected) |
79 | 81 |
|
80 | 82 | command = ["cat", "--a-long-option={file}", "-a", "test2"] |
81 | 83 | expected = ["cat", "--a-long-option=test", "-a", "test2"] |
82 | | - self.assertTrue(replaceFileString(command, ["test"])) |
| 84 | + self.assertTrue(_replaceFileStringInCommand(command, ["test"])) |
83 | 85 | self.assertEqual(command, expected) |
84 | 86 |
|
85 | 87 | command = ["cat", "--input={file}", "-a", "test3"] |
86 | 88 | expected = ["cat", "--input=test1", "--input=test2", "-a", "test3"] |
87 | | - self.assertTrue(replaceFileString(command, ["test1", "test2"])) |
| 89 | + self.assertTrue(_replaceFileStringInCommand(command, ["test1", "test2"])) |
88 | 90 | self.assertEqual(command, expected) |
89 | 91 |
|
90 | 92 |
|
| 93 | + def testReplaceFileStringInSpring(self): |
| 94 | + """Verify that the _replaceFileStringInSpring function works as expected.""" |
| 95 | + commands = [["cat", "test"]] |
| 96 | + self.assertFalse(_replaceFileStringInSpring(commands, ["test"])) |
| 97 | + |
| 98 | + commands = [["cat", "-o", "test"]] |
| 99 | + self.assertFalse(_replaceFileStringInSpring(commands, ["test"])) |
| 100 | + |
| 101 | + commands = [["cat", "{file}"]] |
| 102 | + expected = [["cat", "test"]] |
| 103 | + self.assertTrue(_replaceFileStringInSpring(commands, ["test"])) |
| 104 | + self.assertEqual(commands, expected) |
| 105 | + |
| 106 | + commands = [["cat", "-o", "{file}", "-a", "test2"]] |
| 107 | + expected = [ |
| 108 | + ["cat", "-o", "test1", "-a", "test2"], |
| 109 | + ["cat", "-o", "test2", "-a", "test2"], |
| 110 | + ["cat", "-o", "test3", "-a", "test2"], |
| 111 | + ] |
| 112 | + files = ["test1", "test2", "test3"] |
| 113 | + self.assertTrue(_replaceFileStringInSpring(commands, files)) |
| 114 | + self.assertEqual(commands, expected) |
| 115 | + |
| 116 | + commands = [["cat", "--a-long-option={file}"]] |
| 117 | + expected = [ |
| 118 | + ["cat", "--a-long-option=1"], |
| 119 | + ["cat", "--a-long-option=2"], |
| 120 | + ["cat", "--a-long-option=42"], |
| 121 | + ] |
| 122 | + self.assertTrue(_replaceFileStringInSpring(commands, ["1", "2", "42"])) |
| 123 | + self.assertEqual(commands, expected) |
| 124 | + |
| 125 | + # We also allow command other than the first to contain the {file} |
| 126 | + # string. |
| 127 | + commands = [["cat", "test"], ["cat", "--a-long-option={file}"]] |
| 128 | + expected = [ |
| 129 | + ["cat", "test"], |
| 130 | + ["cat", "--a-long-option=1"], |
| 131 | + ["cat", "--a-long-option=2"], |
| 132 | + ["cat", "--a-long-option=42"], |
| 133 | + ] |
| 134 | + self.assertTrue(_replaceFileStringInSpring(commands, ["1", "2", "42"])) |
| 135 | + self.assertEqual(commands, expected) |
| 136 | + |
| 137 | + |
| 138 | + def testReplaceFileStringSuccess(self): |
| 139 | + """Verify that the replaceFileString function works as expected.""" |
| 140 | + # Test the function with a "normal" command. |
| 141 | + command = ["cat", "{file}"] |
| 142 | + expected = ["cat", "test"] |
| 143 | + self.assertEqual(replaceFileString(command, ["test"]), expected) |
| 144 | + |
| 145 | + # Test the function with a spring. |
| 146 | + command = [["cat", "{file}"], ["echo", "success"]] |
| 147 | + expected = [["cat", "test"], ["echo", "success"]] |
| 148 | + self.assertEqual(replaceFileString(command, ["test"]), expected) |
| 149 | + |
| 150 | + |
| 151 | + def testReplaceFileStringFailure(self): |
| 152 | + """Verify that the replaceFileString function throws correct errors.""" |
| 153 | + regex = r"Replacement string.*in: \"%s\"$" |
| 154 | + command = [ |
| 155 | + ["cat", "test1"], |
| 156 | + ["cat", "test2"], |
| 157 | + ] |
| 158 | + with self.assertRaisesRegex(NameError, regex % "cat test1 | cat test2"): |
| 159 | + replaceFileString(command, ["test"]) |
| 160 | + |
| 161 | + command = ["echo", "data"] |
| 162 | + with self.assertRaisesRegex(NameError, regex % "echo data"): |
| 163 | + replaceFileString(command, ["test"]) |
| 164 | + |
| 165 | + |
91 | 166 | if __name__ == "__main__": |
92 | 167 | main() |
0 commit comments