How to use __test method in storybook-test-runner

Best JavaScript code snippet using storybook-test-runner

test-fixed.py

Source:test-fixed.py Github

copy

Full Screen

1#!/usr/bin/python2#3# A test suite for clash. Designed as unit tests for John Ousterhout's4# implementation, but implemented at a high enough level that it can be5# applied to other implementations as well.6import os7import shutil8import subprocess9import sys10import time11import unittest12# The location of the shell to be tested. You can change this to13# /bin/bash to see if Bash passes this test suite.14shell = "./clash"15# shell = "/bin/bash"16# This variable will hold the exit status of the most recent command17# executed by "run"18status = 019def readFile(name):20 """ Return the contents of a file. """21 f = open(name, "r")22 result = f.read()23 f.close()24 return result25def writeFile(name, contents="xyzzy"):26 """ Create a file with particular contents. """27 f = open(name, "w")28 f.write(contents)29 f.close()30def run(cmd):31 """ Invoke the test shell with the given command and return any32 output generated.33 """34 global status35 writeFile("__stdin", cmd)36 stdin = open("__stdin", "r")37 # This sometime fails under Cygwin, so try again when that happens38 try:39 stdout = open("__stdout", "w")40 except:41 time.sleep(0.01)42 stdout = open("__stdout", "w")43 status = subprocess.call(shell, stdin=stdin, stdout=stdout,44 stderr=subprocess.STDOUT)45 result = readFile("__stdout")46 stdin.close()47 stdout.close()48 os.remove("__stdin")49 # This sometime fails under Cygwin, so try again when that happens50 try:51 os.remove("__stdout")52 except:53 time.sleep(0.01)54 os.remove("__stdout")55 return result56def runWithArgs(*args):57 """ Invoke the test shell with the given set of arguments, and58 return any output generated.59 """60 global status61 # This sometime fails under Cygwin, so try again when that happens62 try:63 stdout = open("__stdout", "w")64 except:65 time.sleep(0.01)66 stdout = open("__stdout", "w")67 fullArgs = []68 fullArgs.append(shell)69 fullArgs.extend(args)70 status = subprocess.call(fullArgs, stdout=stdout,71 stderr=subprocess.STDOUT)72 result = readFile("__stdout")73 stdout.close()74 # This sometime fails under Cygwin, so try again when that happens75 try:76 os.remove("__stdout")77 except:78 time.sleep(0.01)79 os.remove("__stdout")80 return result81class TestBuiltin(unittest.TestCase):82 def tearDown(self):83 if os.path.exists("__test"):84 shutil.rmtree("__test")85 def test_cd_no_args(self):86 self.assertEqual(run("cd ~; pwd"), run("cd; pwd"))87 def test_cd_no_HOME(self):88 self.assertIn("cd: HOME not set", run("unset HOME; cd"))89 def test_cd_HOME_bad_path(self):90 self.assertIn("__bogus/foo: No such file or directory",91 run("HOME=__bogus/foo; cd"))92 def test_cd_too_many_args(self):93 self.assertIn("cd: Too many arguments", run("cd a b"))94 def test_cd_bad_path(self):95 self.assertIn("__bogus/foo: No such file or directory",96 run("cd __bogus/foo"))97 def test_cd_success(self):98 os.makedirs("__test")99 writeFile("__test/foo", "abc def");100 self.assertEqual("abc def",101 run("cd __test; /bin/cat foo"))102 def test_exit_no_args(self):103 self.assertEqual("", run("exit; echo foo"))104 self.assertEqual(0, status)105 def test_exit_with_arg(self):106 self.assertEqual("", run("exit 14; echo foo"))107 self.assertEqual(14, status)108 def test_exit_non_numeric_arg(self):109 self.assertIn("jkl: Numeric argument required",110 run("exit jkl; echo foo"))111 self.assertEqual(2, status)112 def test_exit_too_many_arguments(self):113 self.assertIn("exit: Too many arguments", run("exit 1 2 3; echo foo"))114 self.assertEqual(1, status)115class TestExpand(unittest.TestCase):116 def tearDown(self):117 if os.path.exists("__test"):118 shutil.rmtree("__test")119 def test_expandPath_no_wildcards(self):120 os.makedirs("__test/foo")121 self.assertEqual("abc def\n", run("/bin/echo abc def"))122 self.assertEqual("__test/[a]*\n", run('/bin/echo __test/"[a]*"'))123 def test_expandPath_file_matched(self):124 os.makedirs("__test/foo")125 self.assertEqual("__test/foo\n", run('/bin/echo __test/[f]*'))126 def test_expandPath_no_file_matched(self):127 os.makedirs("__test/foo");128 self.assertEqual("__test/x*\n", run('/bin/echo __test/x*'))129 def test_expandPath_multiple_files_matched(self):130 os.makedirs("__test/foo")131 writeFile("__test/foo/a.c")132 writeFile("__test/foo/b.c")133 writeFile("__test/foo/c.cc")134 result = run('/bin/echo __test/foo/*.c')135 self.assertIn("__test/foo/a.c", result)136 self.assertIn("__test/foo/b.c", result)137 def test_expandTilde(self):138 self.assertEqual("/home/ouster\n",139 run("HOME=/home/ouster; /bin/echo ~\n"))140 self.assertEqual("/home/ouster/xyz\n", run("HOME=/home/ouster; /bin/echo ~/xyz\n"))141 self.assertEqual("/var/root\n", run("/bin/echo ~root\n"))142 self.assertEqual("/var/root/xyz\n", run("/bin/echo ~root/xyz\n"))143 self.assertEqual("~__bogus__/xyz\n", run("HOME=/home/ouster; /bin/echo ~__bogus__/xyz\n"))144 def test_matchFiles_bad_directory(self):145 self.assertEqual("__test/bogus/*\n", run('/bin/echo __test/bogus/*'))146 def test_matchFiles_no_match_in_directory(self):147 os.makedirs("__test")148 self.assertEqual("__test/*.c\n", run('/bin/echo __test/*.c'))149 def test_matchFiles_repeated_separators(self):150 os.makedirs("__test/foo/bar")151 self.assertEqual("__test/foo\n", run('/bin/echo __t*//foo'))152 self.assertEqual("__test/foo/bar\n", run('/bin/echo __t*//f*//bar'))153 self.assertEqual("__test//foo/bar\n", run('/bin/echo __test//f*//bar'))154 def test_matchFiles_multiple_levels_of_matching(self):155 os.makedirs("__test/x1")156 os.makedirs("__test/x2")157 writeFile("__test/x1/a.c")158 writeFile("__test/x2/b.c")159 writeFile("__test/x2/c.c")160 result = run('/bin/echo __test/x?/*.c')161 self.assertIn("__test/x1/a.c", result)162 self.assertIn("__test/x2/b.c", result)163 self.assertIn("__test/x2/c.c", result)164 def test_matchString_fail_end_of_string(self):165 os.makedirs("__test")166 writeFile("__test/xyz")167 self.assertEqual("__tes?/xyzq\n", run('/bin/echo __tes?/xyzq'))168 def test_matchString_question_mark(self):169 os.makedirs("__test")170 writeFile("__test/xyz")171 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x?z'))172 self.assertEqual("__tes?/x?z\n", run('/bin/echo __tes?/x\?z'))173 def test_matchString_asterisk(self):174 os.makedirs("__test")175 writeFile("__test/xyz")176 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/*z'))177 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x*z'))178 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x*'))179 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x****yz'))180 self.assertEqual("__tes?/x*z\n", run('/bin/echo __tes?/x\*z'))181 def test_matchString_brackets(self):182 os.makedirs("__test")183 writeFile("__test/xyz")184 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x[ayql]z'))185 self.assertEqual("__tes?/x[abc]z\n", run('/bin/echo __tes?/x[abc]z'))186 self.assertEqual("__tes?/x[y]z\n", run('/bin/echo __tes?/x\[y]z'))187 def test_matchString_character_mismatch(self):188 os.makedirs("__test")189 writeFile("__test/xyz")190 self.assertEqual("__tes?/xa*\n", run('/bin/echo __tes?/xa*'))191 def test_matchString_pattern_ends_before_string(self):192 os.makedirs("__test")193 writeFile("__test/xyz")194 self.assertEqual("__tes?/xy\n", run('/bin/echo __tes?/xy'))195 def test_matchBrackets(self):196 os.makedirs("__test")197 writeFile("__test/testFile")198 writeFile("__test/te[st")199 self.assertEqual("__test/testFile\n",200 run('/bin/echo __test/te[qrstu]tFile'))201 self.assertEqual("__test/testFile\n",202 run('/bin/echo __test/te[s-u]tFile'))203 self.assertEqual("__test/testFile\n",204 run('/bin/echo __test/te[c-u]tFile'))205 self.assertEqual("__test/testFile\n",206 run('/bin/echo __test/te[c-s]tFile'))207 self.assertEqual("__test/testFile\n",208 run('/bin/echo __test/te[xa-el-u]tFile'))209 self.assertEqual("__test/te[^q-u]tFile\n",210 run('/bin/echo __test/te[^q-u]tFile'))211 self.assertEqual("__test/testFile\n",212 run('/bin/echo __test/te[^q-r]tFile'))213 self.assertEqual("__test/te[st\n",214 run('/bin/echo __test/te[[]*'))215class TestMain(unittest.TestCase):216 def tearDown(self):217 if os.path.exists("__test"):218 shutil.rmtree("__test")219 def test_loadArgs(self):220 os.makedirs("__test");221 writeFile("__test/script", "/bin/echo $0 $1 $2 '|' $# '|' $*")222 self.assertEqual("__test/script a b | 3 | a b c\n",223 runWithArgs("__test/script", "a", "b", "c"))224 self.assertEqual("0 | " + shell + " |\n",225 run("/bin/echo $# '|' $0 $1 '|' $*"))226 def test_main_c_option_basics(self):227 self.assertEqual("foo bar\n", runWithArgs("-c", "/bin/echo foo bar"))228 def test_main_c_option_missing_arg(self):229 self.assertIn("option requires an argument", runWithArgs("-c"))230 self.assertEqual(2, status)231 def test_main_c_option_exit_code(self):232 self.assertEqual("", runWithArgs("-c", "exit 44"))233 self.assertEqual(44, status)234 def test_main_script_file_basics(self):235 os.makedirs("__test");236 writeFile("__test/script", "/bin/echo 'foo\nbar'\n/bin/echo second command")237 self.assertEqual("foo\nbar\nsecond command\n",238 runWithArgs("__test/script"))239 def test_main_script_file_nonexistent_file(self):240 self.assertIn("_bogus_/xyzzy: No such file or directory\n",241 runWithArgs("_bogus_/xyzzy"))242 self.assertEqual(127, status)243 def test_main_script_file_exit_status(self):244 os.makedirs("__test");245 writeFile("__test/script", "/bin/echo command output\nexit 32\n \n")246 self.assertEqual("command output\n", runWithArgs("__test/script"))247 self.assertEqual(32, status)248 def test_main_script_file_command_line_args(self):249 os.makedirs("__test");250 writeFile("__test/script", "/bin/echo $# '|' $0 $1 '|' $*\n")251 self.assertEqual("4 | __test/script a b | a b c d e\n",252 runWithArgs("__test/script", "a b", "c", "d", "e"))253 self.assertEqual(0, status)254class TestParser(unittest.TestCase):255 def tearDown(self):256 if os.path.exists("__test"):257 shutil.rmtree("__test")258 def test_eval_basics(self):259 self.assertEqual("foo abc $y\n", run("x=foo; /bin/echo $x abc '$y'"))260 def test_eval_input_file_subs(self):261 os.makedirs("__test")262 writeFile("__test/foo", "abcde");263 self.assertEqual("abcde", run("x=foo; /bin/cat <__test/$x"))264 def test_eval_output_file_subs(self):265 os.makedirs("__test")266 self.assertEqual("", run("x=foo; /bin/echo foo bar >__test/$x"))267 self.assertEqual("foo bar\n", readFile("__test/foo"))268 def test_eval_errors(self):269 self.assertIn("unexpected EOF while looking for matching `''",270 run("/bin/echo 'a b c"))271 self.assertIn("${}: bad substitution", run("/bin/echo a b ${}"))272 output = run("/bin/echo start; echo ${}")273 self.assertIn("${}: bad substitution", output)274 self.assertIn("start\n", output)275 def test_doSubs_tildes_first(self):276 home = run("/bin/echo ~")[:-1]277 self.assertNotIn("~", home)278 self.assertEqual("%s ~ ~ ~ ~\n"% (home),279 run("x='~ ~'; /bin/echo ~ $x \"~\" '~'"))280 def test_doSubs_variables(self):281 self.assertEqual("Word 1: foo\nWord 2: a\n"282 + "Word 3: b\nWord 4: a b\n",283 run("x=foo; y='a b'; ./words.py $x $y \"$y\""))284 self.assertEqual("a\nb\n", run("x='a\nb'; /bin/echo \"$x\""))285 self.assertEqual("Word 1: a\nWord 2: b\nWord 3: c\n",286 run("x='a b c '; ./words.py $x"))287 self.assertEqual("", run("x=''; ./words.py $x $x"))288 self.assertEqual("Word 1: .\nWord 2: a\nWord 3: b\nWord 4: .\n",289 run("x=' a b '; ./words.py .$x."))290 def test_doSubs_commands(self):291 self.assertEqual("abc\n", run("x=abc; /bin/echo `/bin/echo $x`"))292 self.assertEqual("$abc\n", run("x='$abc'; /bin/echo `/bin/echo $x`"))293 def test_doSubs_backslashes(self):294 self.assertEqual("$x \"$x\n", run("x=abc; /bin/echo \$x \"\\\"\\$x\""))295 def test_doSubs_double_quotes(self):296 self.assertEqual("Word 1: a x y z b\n",297 run('./words.py "a `/bin/echo x y z` b"'))298 self.assertEqual("Word 1: \nWord 2: \nWord 3: \n",299 run("x=\"\"; ./words.py $x\"\" \"\" \"$x\""))300 def test_doSubs_single_quotes(self):301 self.assertEqual("Word 1: a $x `echo foo`\n",302 run("x=abc; ./words.py 'a $x `echo foo`'"))303 self.assertEqual("Word 1: \nWord 2: \nWord 3: \n",304 run("x=''; ./words.py $x'' ''$x ''"))305 def test_doSubs_path_expansion(self):306 os.makedirs("__test")307 writeFile("__test/a.c")308 writeFile("__test/b.c")309 result = run("x='*.c'; /bin/echo __test/*.c")310 self.assertIn("__test/a.c", result)311 self.assertIn("__test/b.c", result)312 result = run("x='*.c'; /bin/echo __test/$x")313 self.assertIn("__test/a.c", result)314 self.assertIn("__test/b.c", result)315 self.assertEqual("__test/*.c\n",316 run("x='*.c'; /bin/echo \"__test/*.c\""))317 def test_parse_leading_separators(self):318 self.assertEqual("a b c\n", run("/bin/echo a b c "))319 def test_parse_single_quotes(self):320 self.assertEqual("Word 1: xyz \\\nWord 2: abc\n",321 run("./words.py 'xyz \\' abc"))322 def test_parse_double_quotes(self):323 self.assertEqual("Word 1: a b c\n",324 run("./words.py \"a b c\""))325 self.assertEqual("Word 1: a b\n",326 run("x='a b'; ./words.py \"$x\""))327 self.assertEqual("Word 1: foo bar\n",328 run("./words.py \"`/bin/echo foo bar`\""))329 self.assertEqual("Word 1: a\\b$x\n",330 run("x=abc; ./words.py \"a\\b\$x\""))331 self.assertEqual("\"\n", run("/bin/echo '\"'"))332 def test_parse_variables(self):333 self.assertEqual("abcyyy\n", run("xxx=abc; /bin/echo ${xxx}yyy"))334 self.assertIn("unexpected EOF while looking for `}'",335 run("/bin/echo ${xxx"))336 self.assertEqual("abc.z\n", run("x0yz4=abc; /bin/echo $x0yz4.z"))337 self.assertEqual("a55b\n", run("/bin/bash -c 'exit 55'; /bin/echo a$?b"))338 self.assertIn("${}: bad substitution\n", run("/bin/echo ${}"))339 self.assertEqual("Word 1: a\nb\nc\n",340 run("x='a\nb\nc'; ./words.py \"$x\""))341 self.assertEqual("$ $x $\n", run("x0yz4=abc; /bin/echo $ \"$\"x $"))342 self.assertEqual("arg12xy\n",343 runWithArgs("-c", "echo $12xy", "arg0", "arg1", "arg2"))344 def test_parse_commands_basics(self):345 self.assertEqual("Word 1: $y\n",346 run("x='$y'; y=abc; /bin/echo `./words.py $x`"))347 self.assertEqual("Word 1: a b c Word 2: x y\n",348 run("/bin/echo `./words.py \"a b c\" 'x y'`"))349 self.assertEqual("`\n", run("/bin/echo '`'"))350 def test_parse_commands_no_word_breaks(self):351 self.assertEqual("Word 1: a b c\n",352 run("./words.py \"`/bin/echo a b c`\""))353 def test_parse_commands_errors(self):354 self.assertIn("Unexpected EOF while looking for matching ``'",355 run("/bin/echo `foo bar"))356 def test_parse_backslashes(self):357 self.assertEqual("aa$x`echo foo`\n",358 run("x=99; /bin/echo a\\a\\$x\\`echo foo\\`"))359 self.assertIn("Unexpected EOF while parsing backslash",360 run("/bin/echo \\"))361 self.assertIn("Unexpected EOF after backslash-newline",362 run("/bin/echo \\\n"))363 self.assertEqual("Word 1: axyz\n", run("./words.py a\\\nxyz"))364 def test_parse_backslashes_in_quotes(self):365 self.assertEqual("a$x`b\"c\\d\ne\\a\n",366 run("x=99; /bin/echo \"a\\$x\\`b\\\"c\\\\d\\\ne\\a\""))367 def test_parse_backslashes_meaningless(self):368 self.assertEqual("a\\b\n", run("/bin/echo 'a\\b'"))369 def test_split_basics(self):370 os.makedirs("__test")371 self.assertEqual("abc def\n", run("/bin/echo abc def"))372 self.assertEqual("", run("/bin/echo abc def > __test/foo"))373 self.assertEqual("", run("/bin/echo abc def>__test/foo"))374 self.assertEqual("abc def\n", readFile("__test/foo"))375 self.assertEqual("abc def\n", run("/bin/cat < __test/foo"))376 self.assertEqual("abc def\n", run("/bin/cat<__test/foo"))377 def test_split_empty_first_word(self):378 os.makedirs("__test")379 self.assertEqual("", run("> __test/foo /bin/echo abc"))380 self.assertEqual("abc\n", readFile("__test/foo"))381 def test_split_missing_input_file(self):382 os.makedirs("__test")383 self.assertEqual("-clash: no file given for input redirection\n",384 run("/bin/echo abc < >__test/foo"))385 def test_split_missing_output_file(self):386 os.makedirs("__test")387 self.assertEqual("-clash: no file given for output redirection\n",388 run("/bin/echo abc >;"))389 def test_split_pipeline(self):390 os.makedirs("__test")391 self.assertEqual("abc def\n", run("/bin/echo abc def | /bin/cat"))392 self.assertEqual("abc def\n", run("/bin/echo abc def|/bin/cat"))393 self.assertEqual(" 1\t 1\tabc def\n",394 run("/bin/echo abc def | /bin/cat -n | /bin/cat -n"))395 def test_split_multiple_pipelines(self):396 os.makedirs("__test")397 self.assertEqual("xyz\n",398 run("/bin/echo abc>__test/out1;/bin/echo def > __test/out2;"399 + "/bin/echo xyz"))400 self.assertEqual("abc\n", readFile("__test/out1"))401 self.assertEqual("def\n", readFile("__test/out2"))402 self.assertEqual("xyz\n",403 run("/bin/echo abc>__test/out1;/bin/echo def > __test/out2 ;"404 + "/bin/echo xyz"))405 self.assertEqual("abc\n", readFile("__test/out1"))406 self.assertEqual("def\n", readFile("__test/out2"))407 def test_breakAndAppend(self):408 self.assertEqual("Word 1: .abc\nWord 2: def\nWord 3: x\nWord 4: y.\n",409 run("x='abc def\tx\ny'; ./words.py .$x."))410 self.assertEqual("Word 1: .\nWord 2: a\nWord 3: b\nWord 4: .\n",411 run("x=' \t\n a b \t\n'; ./words.py .$x."))412class TestPipeline(unittest.TestCase):413 # Much of the code in this class was already tested by other414 # tests, such as those for Parser.415 def tearDown(self):416 if os.path.exists("__test"):417 shutil.rmtree("__test")418 def test_rebuildPathMap_basics(self):419 os.makedirs("__test")420 os.makedirs("__test/child")421 writeFile("__test/a", "#!/bin/sh\n/bin/echo __test/a")422 os.chmod("__test/a", 0o777)423 writeFile("__test/b", "#!/bin/sh\n/bin/echo __test/b")424 os.chmod("__test/b", 0o777)425 writeFile("__test/child/a", "#!/bin/sh\n/bin/echo __test/child/a")426 os.chmod("__test/child/a", 0o777)427 writeFile("__test/child/b", "#!/bin/sh\n/bin/echo __test/child/b")428 self.assertEqual("__test/child/a\n__test/b\n",429 run("PATH=\"`/bin/pwd`/__test/child:`/bin/pwd`/__test\"; a; b"))430 def test_rebuildPathMap_default(self):431 self.assertEqual("a b\n", run("unset PATH; echo a b"))432 def test_run_redirection_basics(self):433 os.makedirs("__test")434 self.assertEqual("a b c\n",435 run("/bin/echo a b c > __test/foo; /bin/cat __test/foo"))436 self.assertEqual("a b c\n", run("/bin/cat < __test/foo"))437 def test_run_ambiguous_input_redirection(self):438 self.assertIn("Ambiguous input redirection",439 run("x='a b'; /bin/cat <$x"))440 def test_run_bad_input_file(self):441 os.makedirs("__test")442 self.assertIn("No such file or directory",443 run("cat < __test/bogus"))444 def test_run_ambiguous_output_redirection(self):445 self.assertIn("Ambiguous output redirection",446 run("x='a b'; /bin/echo foo bar >$x"))447 def test_run_pipeline(self):448 self.assertIn("x y z\n", run("/bin/echo x y z | cat"))449 def test_run_bad_output_file(self):450 os.makedirs("__test")451 self.assertIn("No such file or directory",452 run("/bin/echo abc > __test/_bogus/xyz"))453 def test_run_rebuild_path_cache_to_discover_new_file(self):454 os.makedirs("__test")455 writeFile("__test/x", "#!/bin/sh\n/bin/echo __test/x")456 self.assertIn(" x: command not found\n__test/x",457 run("PATH=\"/bin:`pwd`/__test\"; x; chmod +x __test/x; x"))458 def test_run_rebuild_path_cache_path_changed(self):459 os.makedirs("__test")460 writeFile("__test/x", "#!/bin/sh\n/bin/echo __test/x")461 os.chmod("__test/x", 0o777)462 self.assertIn(" x: command not found\n__test/x",463 run("x; PATH=\"/bin:`pwd`/__test\"; x"))464 def test_run_no_such_executable(self):465 os.makedirs("__test")466 self.assertIn("No such file or directory", run("__test/bogus foo bar"))467 self.assertIn("No such file or directory",468 run("__test/bogus foo bar | /bin/echo foo"))469 self.assertNotIn("No such file or directory",470 run("__test/bogus foo bar |& /bin/echo foo"))471 self.assertEqual("",472 run("__test/bogus foo bar |& /bin/cat > __test/out"))473 self.assertIn("No such file or directory",474 readFile("__test/out"))475 def test_run_set_status_variable(self):476 os.makedirs("__test")477 self.assertEqual("0\n", run("/usr/bin/true; /bin/echo $?"))478 self.assertEqual("44\n", run("/bin/bash -c \"exit 44\"; /bin/echo $?"))479class TestVariables(unittest.TestCase):480 def test_set(self):481 self.assertEqual("50 100\n", run("x=99; y=100; x=50; /bin/echo $x $y"))482 self.assertIn("x=100",483 run("x=99; export x; /bin/echo foo; x=100; /usr/bin/printenv"))484 def test_setFromEnviron(self):485 self.assertNotEqual("\n", run("/bin/echo $HOME"))486 self.assertNotEqual("\n", run("/bin/echo $SHELL"))487 self.assertIn("SHELL=xyzzy", run("SHELL=xyzzy; /usr/bin/printenv"))488 def test_set(self):489 self.assertEqual(".99. ..\n", run("x=99; /bin/echo .$x. .$y."))490 def test_getEnviron(self):491 result = run("x=99; export x; y=100; /usr/bin/printenv")492 self.assertIn("x=99", result)493 self.assertNotIn("y=", result)494 self.assertIn("HOME=", result)495 self.assertIn("SHELL=", result)496 def test_unset(self):497 self.assertEqual("~99~\n~~\n",498 run("x=99; /bin/echo ~$x~; unset x; /bin/echo ~$x~"))499 result = run("x=99; export x; echo x:$x; unset x; /usr/bin/printenv")500 self.assertIn("x:99", result);501 self.assertNotIn("x=99", result);502 def test_markExported(self):503 self.assertNotIn("x=99", run("x=99; /usr/bin/printenv"))504 self.assertIn("x=99", run("x=99; /bin/echo foo bar; export x; "505 "/usr/bin/printenv"))506if __name__ == '__main__':...

Full Screen

Full Screen

test.py

Source:test.py Github

copy

Full Screen

1#!/usr/bin/python2#3# A test suite for clash. Designed as unit tests for John Ousterhout's4# implementation, but implemented at a high enough level that it can be5# applied to other implementations as well.6import os7import shutil8import subprocess9import sys10import time11import unittest12# The location of the shell to be tested. You can change this to13# /bin/bash to see if Bash passes this test suite.14shell = "./clash"15#shell = "/bin/bash"16# This variable will hold the exit status of the most recent command17# executed by "run"18status = 019def readFile(name):20 """ Return the contents of a file. """21 f = open(name, "r")22 result = f.read()23 f.close()24 return result25def writeFile(name, contents="xyzzy"):26 """ Create a file with particular contents. """27 f = open(name, "w")28 f.write(contents)29 f.close()30def run(cmd):31 """ Invoke the test shell with the given command and return any32 output generated.33 """34 global status35 writeFile("__stdin", cmd)36 stdin = open("__stdin", "r")37 # This sometime fails under Cygwin, so try again when that happens38 try:39 stdout = open("__stdout", "w")40 except:41 time.sleep(0.01)42 stdout = open("__stdout", "w")43 status = subprocess.call(shell, stdin=stdin, stdout=stdout,44 stderr=subprocess.STDOUT)45 result = readFile("__stdout")46 stdin.close()47 stdout.close()48 os.remove("__stdin")49 # This sometime fails under Cygwin, so try again when that happens50 try:51 os.remove("__stdout")52 except:53 time.sleep(0.01)54 os.remove("__stdout")55 return result56def runWithArgs(*args):57 """ Invoke the test shell with the given set of arguments, and58 return any output generated.59 """60 global status61 # This sometime fails under Cygwin, so try again when that happens62 try:63 stdout = open("__stdout", "w")64 except:65 time.sleep(0.01)66 stdout = open("__stdout", "w")67 fullArgs = []68 fullArgs.append(shell)69 fullArgs.extend(args)70 status = subprocess.call(fullArgs, stdout=stdout,71 stderr=subprocess.STDOUT)72 result = readFile("__stdout")73 stdout.close()74 # This sometime fails under Cygwin, so try again when that happens75 try:76 os.remove("__stdout")77 except:78 time.sleep(0.01)79 os.remove("__stdout")80 return result81class TestBuiltin(unittest.TestCase):82 def tearDown(self):83 if os.path.exists("__test"):84 shutil.rmtree("__test")85 def test_cd_no_args(self):86 self.assertEqual(run("cd ~; pwd"), run("cd; pwd"))87 def test_cd_no_HOME(self):88 self.assertIn("cd: HOME not set", run("unset HOME; cd"))89 def test_cd_HOME_bad_path(self):90 self.assertIn("__bogus/foo: No such file or directory",91 run("HOME=__bogus/foo; cd"))92 def test_cd_too_many_args(self):93 self.assertIn("cd: Too many arguments", run("cd a b"))94 def test_cd_bad_path(self):95 self.assertIn("__bogus/foo: No such file or directory",96 run("cd __bogus/foo"))97 def test_cd_success(self):98 os.makedirs("__test")99 writeFile("__test/foo", "abc def");100 self.assertEqual("abc def",101 run("cd __test; /bin/cat foo"))102 def test_exit_no_args(self):103 self.assertEqual("", run("exit; echo foo"))104 self.assertEqual(0, status)105 def test_exit_with_arg(self):106 self.assertEqual("", run("exit 14; echo foo"))107 self.assertEqual(14, status)108 def test_exit_non_numeric_arg(self):109 self.assertIn("jkl: Numeric argument required",110 run("exit jkl; echo foo"))111 self.assertEqual(2, status)112 def test_exit_too_many_arguments(self):113 self.assertIn("exit: Too many arguments", run("exit 1 2 3; echo foo"))114 self.assertEqual(1, status)115class TestExpand(unittest.TestCase):116 def tearDown(self):117 if os.path.exists("__test"):118 shutil.rmtree("__test")119 def test_expandPath_no_wildcards(self):120 os.makedirs("__test/foo")121 self.assertEqual("abc def\n", run("/bin/echo abc def"))122 self.assertEqual("__test/[a]*\n", run('/bin/echo __test/"[a]*"'))123 def test_expandPath_file_matched(self):124 os.makedirs("__test/foo")125 self.assertEqual("__test/foo\n", run('/bin/echo __test/[f]*'))126 def test_expandPath_no_file_matched(self):127 os.makedirs("__test/foo");128 self.assertEqual("__test/x*\n", run('/bin/echo __test/x*'))129 def test_expandPath_multiple_files_matched(self):130 os.makedirs("__test/foo")131 writeFile("__test/foo/a.c")132 writeFile("__test/foo/b.c")133 writeFile("__test/foo/c.cc")134 result = run('/bin/echo __test/foo/*.c')135 self.assertIn("__test/foo/a.c", result)136 self.assertIn("__test/foo/b.c", result)137 def test_expandTilde(self):138 self.assertEqual("/home/ouster\n",139 run("PATH=/home/ouster; /bin/echo ~\n"))140 self.assertEqual("/home/ouster/xyz\n", run("/bin/echo ~/xyz\n"))141 self.assertEqual("/home/ouster\n", run("/bin/echo ~ouster\n"))142 self.assertEqual("/home/ouster/xyz\n", run("/bin/echo ~ouster/xyz\n"))143 self.assertEqual("~__bogus__/xyz\n", run("/bin/echo ~__bogus__/xyz\n"))144 def test_matchFiles_bad_directory(self):145 self.assertEqual("__test/bogus/*\n", run('/bin/echo __test/bogus/*'))146 def test_matchFiles_no_match_in_directory(self):147 os.makedirs("__test")148 self.assertEqual("__test/*.c\n", run('/bin/echo __test/*.c'))149 def test_matchFiles_repeated_separators(self):150 os.makedirs("__test/foo/bar")151 self.assertEqual("__test/foo\n", run('/bin/echo __t*//foo'))152 self.assertEqual("__test/foo/bar\n", run('/bin/echo __t*//f*//bar'))153 self.assertEqual("__test//foo/bar\n", run('/bin/echo __test//f*//bar'))154 def test_matchFiles_multiple_levels_of_matching(self):155 os.makedirs("__test/x1")156 os.makedirs("__test/x2")157 writeFile("__test/x1/a.c")158 writeFile("__test/x2/b.c")159 writeFile("__test/x2/c.c")160 result = run('/bin/echo __test/x?/*.c')161 self.assertIn("__test/x1/a.c", result)162 self.assertIn("__test/x2/b.c", result)163 self.assertIn("__test/x2/c.c", result)164 def test_matchString_fail_end_of_string(self):165 os.makedirs("__test")166 writeFile("__test/xyz")167 self.assertEqual("__tes?/xyzq\n", run('/bin/echo __tes?/xyzq'))168 def test_matchString_question_mark(self):169 os.makedirs("__test")170 writeFile("__test/xyz")171 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x?z'))172 self.assertEqual("__tes?/x?z\n", run('/bin/echo __tes?/x\?z'))173 def test_matchString_asterisk(self):174 os.makedirs("__test")175 writeFile("__test/xyz")176 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/*z'))177 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x*z'))178 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x*'))179 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x****yz'))180 self.assertEqual("__tes?/x*z\n", run('/bin/echo __tes?/x\*z'))181 def test_matchString_brackets(self):182 os.makedirs("__test")183 writeFile("__test/xyz")184 self.assertEqual("__test/xyz\n", run('/bin/echo __tes?/x[ayql]z'))185 self.assertEqual("__tes?/x[abc]z\n", run('/bin/echo __tes?/x[abc]z'))186 self.assertEqual("__tes?/x[y]z\n", run('/bin/echo __tes?/x\[y]z'))187 def test_matchString_character_mismatch(self):188 os.makedirs("__test")189 writeFile("__test/xyz")190 self.assertEqual("__tes?/xa*\n", run('/bin/echo __tes?/xa*'))191 def test_matchString_pattern_ends_before_string(self):192 os.makedirs("__test")193 writeFile("__test/xyz")194 self.assertEqual("__tes?/xy\n", run('/bin/echo __tes?/xy'))195 def test_matchBrackets(self):196 os.makedirs("__test")197 writeFile("__test/testFile")198 writeFile("__test/te[st")199 self.assertEqual("__test/testFile\n",200 run('/bin/echo __test/te[qrstu]tFile'))201 self.assertEqual("__test/testFile\n",202 run('/bin/echo __test/te[s-u]tFile'))203 self.assertEqual("__test/testFile\n",204 run('/bin/echo __test/te[c-u]tFile'))205 self.assertEqual("__test/testFile\n",206 run('/bin/echo __test/te[c-s]tFile'))207 self.assertEqual("__test/testFile\n",208 run('/bin/echo __test/te[xa-el-u]tFile'))209 self.assertEqual("__test/te[^q-u]tFile\n",210 run('/bin/echo __test/te[^q-u]tFile'))211 self.assertEqual("__test/testFile\n",212 run('/bin/echo __test/te[^q-r]tFile'))213 self.assertEqual("__test/te[st\n",214 run('/bin/echo __test/te[[]*'))215class TestMain(unittest.TestCase):216 def tearDown(self):217 if os.path.exists("__test"):218 shutil.rmtree("__test")219 def test_loadArgs(self):220 os.makedirs("__test");221 writeFile("__test/script", "/bin/echo $0 $1 $2 '|' $# '|' $*")222 self.assertEqual("__test/script a b | 3 | a b c\n",223 runWithArgs("__test/script", "a", "b", "c"))224 self.assertEqual("0 | |\n",225 run("/bin/echo $# '|' $0 $1 '|' $*"))226 def test_main_c_option_basics(self):227 self.assertEqual("foo bar\n", runWithArgs("-c", "/bin/echo foo bar"))228 def test_main_c_option_missing_arg(self):229 self.assertIn("option requires an argument", runWithArgs("-c"))230 self.assertEqual(2, status)231 def test_main_c_option_exit_code(self):232 self.assertEqual("", runWithArgs("-c", "exit 44"))233 self.assertEqual(44, status)234 def test_main_script_file_basics(self):235 os.makedirs("__test");236 writeFile("__test/script", "/bin/echo 'foo\nbar'\n/bin/echo second command")237 self.assertEqual("foo\nbar\nsecond command\n",238 runWithArgs("__test/script"))239 def test_main_script_file_nonexistent_file(self):240 self.assertIn("_bogus_/xyzzy: No such file or directory\n",241 runWithArgs("_bogus_/xyzzy"))242 self.assertEqual(127, status)243 def test_main_script_file_exit_status(self):244 os.makedirs("__test");245 writeFile("__test/script", "/bin/echo command output\nexit 32\n \n")246 self.assertEqual("command output\n", runWithArgs("__test/script"))247 self.assertEqual(32, status)248 def test_main_script_file_command_line_args(self):249 os.makedirs("__test");250 writeFile("__test/script", "/bin/echo $# '|' $0 $1 '|' $*\n")251 self.assertEqual("4 | __test/script a b | a b c d e\n",252 runWithArgs("__test/script", "a b", "c", "d", "e"))253 self.assertEqual(0, status)254class TestParser(unittest.TestCase):255 def tearDown(self):256 if os.path.exists("__test"):257 shutil.rmtree("__test")258 def test_eval_basics(self):259 self.assertEqual("foo abc $y\n", run("x=foo; /bin/echo $x abc '$y'"))260 def test_eval_input_file_subs(self):261 os.makedirs("__test")262 writeFile("__test/foo", "abcde");263 self.assertEqual("abcde", run("x=foo; /bin/cat <__test/$x"))264 def test_eval_output_file_subs(self):265 os.makedirs("__test")266 self.assertEqual("", run("x=foo; /bin/echo foo bar >__test/$x"))267 self.assertEqual("foo bar\n", readFile("__test/foo"))268 def test_eval_errors(self):269 self.assertIn("unexpected EOF while looking for matching `''",270 run("/bin/echo 'a b c"))271 self.assertIn("${}: bad substitution", run("/bin/echo a b ${}"))272 output = run("/bin/echo start; echo ${}")273 self.assertIn("${}: bad substitution", output)274 self.assertIn("start\n", output)275 def test_doSubs_tildes_first(self):276 home = run("/bin/echo ~")[:-1]277 self.assertNotIn("~", home)278 self.assertEqual("%s ~ ~ ~ ~\n"% (home),279 run("x='~ ~'; /bin/echo ~ $x \"~\" '~'"))280 def test_doSubs_variables(self):281 self.assertEqual("Word 1: foo\nWord 2: a\n"282 + "Word 3: b\nWord 4: a b\n",283 run("x=foo; y='a b'; ./words.py $x $y \"$y\""))284 self.assertEqual("a\nb\n", run("x='a\nb'; /bin/echo \"$x\""))285 self.assertEqual("Word 1: a\nWord 2: b\nWord 3: c\n",286 run("x='a b c '; ./words.py $x"))287 self.assertEqual("", run("x=''; ./words.py $x $x"))288 self.assertEqual("Word 1: .\nWord 2: a\nWord 3: b\nWord 4: .\n",289 run("x=' a b '; ./words.py .$x."))290 def test_doSubs_commands(self):291 self.assertEqual("abc\n", run("x=abc; /bin/echo `/bin/echo $x`"))292 self.assertEqual("$abc\n", run("x='$abc'; /bin/echo `/bin/echo $x`"))293 def test_doSubs_backslashes(self):294 self.assertEqual("$x \"$x\n", run("x=abc; /bin/echo \$x \"\\\"\\$x\""))295 def test_doSubs_double_quotes(self):296 self.assertEqual("Word 1: a x y z b\n",297 run('./words.py "a `/bin/echo x y z` b"'))298 self.assertEqual("Word 1: \nWord 2: \nWord 3: \n",299 run("x=\"\"; ./words.py $x\"\" \"\" \"$x\""))300 def test_doSubs_single_quotes(self):301 self.assertEqual("Word 1: a $x `echo foo`\n",302 run("x=abc; ./words.py 'a $x `echo foo`'"))303 self.assertEqual("Word 1: \nWord 2: \nWord 3: \n",304 run("x=''; ./words.py $x'' ''$x ''"))305 def test_doSubs_path_expansion(self):306 os.makedirs("__test")307 writeFile("__test/a.c")308 writeFile("__test/b.c")309 result = run("x='*.c'; /bin/echo __test/*.c")310 self.assertIn("__test/a.c", result)311 self.assertIn("__test/b.c", result)312 result = run("x='*.c'; /bin/echo __test/$x")313 self.assertIn("__test/a.c", result)314 self.assertIn("__test/b.c", result)315 self.assertEqual("__test/*.c\n",316 run("x='*.c'; /bin/echo \"__test/*.c\""))317 def test_parse_leading_separators(self):318 self.assertEqual("a b c\n", run("/bin/echo a b c "))319 def test_parse_single_quotes(self):320 self.assertEqual("Word 1: xyz \\\nWord 2: abc\n",321 run("./words.py 'xyz \\' abc"))322 def test_parse_double_quotes(self):323 self.assertEqual("Word 1: a b c\n",324 run("./words.py \"a b c\""))325 self.assertEqual("Word 1: a b\n",326 run("x='a b'; ./words.py \"$x\""))327 self.assertEqual("Word 1: foo bar\n",328 run("./words.py \"`/bin/echo foo bar`\""))329 self.assertEqual("Word 1: a\\b$x\n",330 run("x=abc; ./words.py \"a\\b\$x\""))331 self.assertEqual("\"\n", run("/bin/echo '\"'"))332 def test_parse_variables(self):333 self.assertEqual("abcyyy\n", run("xxx=abc; /bin/echo ${xxx}yyy"))334 self.assertIn("unexpected EOF while looking for `}'",335 run("/bin/echo ${xxx"))336 self.assertEqual("abc.z\n", run("x0yz4=abc; /bin/echo $x0yz4.z"))337 self.assertEqual("a55b\n", run("/bin/bash -c 'exit 55'; /bin/echo a$?b"))338 self.assertIn("${}: bad substitution\n", run("/bin/echo ${}"))339 self.assertEqual("Word 1: a\nb\nc\n",340 run("x='a\nb\nc'; ./words.py \"$x\""))341 self.assertEqual("$ $x $\n", run("x0yz4=abc; /bin/echo $ \"$\"x $"))342 self.assertEqual("arg12xy\n",343 runWithArgs("-c", "echo $12xy", "arg0", "arg1", "arg2"))344 def test_parse_commands_basics(self):345 self.assertEqual("Word 1: $y\n",346 run("x='$y'; y=abc; /bin/echo `./words.py $x`"))347 self.assertEqual("Word 1: a b c Word 2: x y\n",348 run("/bin/echo `./words.py \"a b c\" 'x y'`"))349 self.assertEqual("`\n", run("/bin/echo '`'"))350 def test_parse_commands_no_word_breaks(self):351 self.assertEqual("Word 1: a b c\n",352 run("./words.py \"`/bin/echo a b c`\""))353 def test_parse_commands_errors(self):354 self.assertIn("Unexpected EOF while looking for matching ``'",355 run("/bin/echo `foo bar"))356 def test_parse_backslashes(self):357 self.assertEqual("aa$x`echo foo`\n",358 run("x=99; /bin/echo a\\a\\$x\\`echo foo\\`"))359 self.assertIn("Unexpected EOF while parsing backslash",360 run("/bin/echo \\"))361 self.assertIn("Unexpected EOF after backslash-newline",362 run("/bin/echo \\\n"))363 self.assertEqual("Word 1: axyz\n", run("./words.py a\\\nxyz"))364 def test_parse_backslashes_in_quotes(self):365 self.assertEqual("a$x`b\"c\\d\ne\\a\n",366 run("x=99; /bin/echo \"a\\$x\\`b\\\"c\\\\d\\\ne\\a\""))367 def test_parse_backslashes_meaningless(self):368 self.assertEqual("a\\b\n", run("/bin/echo 'a\\b'"))369 def test_split_basics(self):370 os.makedirs("__test")371 self.assertEqual("abc def\n", run("/bin/echo abc def"))372 self.assertEqual("", run("/bin/echo abc def > __test/foo"))373 self.assertEqual("", run("/bin/echo abc def>__test/foo"))374 self.assertEqual("abc def\n", readFile("__test/foo"))375 self.assertEqual("abc def\n", run("/bin/cat < __test/foo"))376 self.assertEqual("abc def\n", run("/bin/cat<__test/foo"))377 def test_split_empty_first_word(self):378 os.makedirs("__test")379 self.assertEqual("", run("> __test/foo /bin/echo abc"))380 self.assertEqual("abc\n", readFile("__test/foo"))381 def test_split_missing_input_file(self):382 os.makedirs("__test")383 self.assertEqual("-clash: no file given for input redirection\n",384 run("/bin/echo abc < >__test/foo"))385 def test_split_missing_output_file(self):386 os.makedirs("__test")387 self.assertEqual("-clash: no file given for output redirection\n",388 run("/bin/echo abc >;"))389 def test_split_pipeline(self):390 os.makedirs("__test")391 self.assertEqual("abc def\n", run("/bin/echo abc def | /bin/cat"))392 self.assertEqual("abc def\n", run("/bin/echo abc def|/bin/cat"))393 self.assertEqual(" 1\t 1\tabc def\n",394 run("/bin/echo abc def | /bin/cat -n | /bin/cat -n"))395 def test_split_multiple_pipelines(self):396 os.makedirs("__test")397 self.assertEqual("xyz\n",398 run("/bin/echo abc>__test/out1;/bin/echo def > __test/out2;"399 + "/bin/echo xyz"))400 self.assertEqual("abc\n", readFile("__test/out1"))401 self.assertEqual("def\n", readFile("__test/out2"))402 self.assertEqual("xyz\n",403 run("/bin/echo abc>__test/out1;/bin/echo def > __test/out2 ;"404 + "/bin/echo xyz"))405 self.assertEqual("abc\n", readFile("__test/out1"))406 self.assertEqual("def\n", readFile("__test/out2"))407 def test_breakAndAppend(self):408 self.assertEqual("Word 1: .abc\nWord 2: def\nWord 3: x\nWord 4: y.\n",409 run("x='abc def\tx\ny'; ./words.py .$x."))410 self.assertEqual("Word 1: .\nWord 2: a\nWord 3: b\nWord 4: .\n",411 run("x=' \t\n a b \t\n'; ./words.py .$x."))412class TestPipeline(unittest.TestCase):413 # Much of the code in this class was already tested by other414 # tests, such as those for Parser.415 def tearDown(self):416 if os.path.exists("__test"):417 shutil.rmtree("__test")418 def test_rebuildPathMap_basics(self):419 os.makedirs("__test")420 os.makedirs("__test/child")421 writeFile("__test/a", "#!/bin/sh\n/bin/echo __test/a")422 os.chmod("__test/a", 0o777)423 writeFile("__test/b", "#!/bin/sh\n/bin/echo __test/b")424 os.chmod("__test/b", 0o777)425 writeFile("__test/child/a", "#!/bin/sh\n/bin/echo __test/child/a")426 os.chmod("__test/child/a", 0o777)427 writeFile("__test/child/b", "#!/bin/sh\n/bin/echo __test/child/b")428 self.assertEqual("__test/child/a\n__test/b\n",429 run("PATH=\"`/bin/pwd`/__test/child:`/bin/pwd`/__test\"; a; b"))430 def test_rebuildPathMap_default(self):431 self.assertEqual("a b\n", run("unset PATH; echo a b"))432 def test_run_redirection_basics(self):433 os.makedirs("__test")434 self.assertEqual("a b c\n",435 run("/bin/echo a b c > __test/foo; /bin/cat __test/foo"))436 self.assertEqual("a b c\n", run("/bin/cat < __test/foo"))437 def test_run_ambiguous_input_redirection(self):438 self.assertIn("Ambiguous input redirection",439 run("x='a b'; /bin/cat <$x"))440 def test_run_bad_input_file(self):441 os.makedirs("__test")442 self.assertIn("No such file or directory",443 run("cat < __test/bogus"))444 def test_run_ambiguous_output_redirection(self):445 self.assertIn("Ambiguous output redirection",446 run("x='a b'; /bin/echo foo bar >$x"))447 def test_run_pipeline(self):448 self.assertIn("x y z\n", run("/bin/echo x y z | cat"))449 def test_run_bad_output_file(self):450 os.makedirs("__test")451 self.assertIn("No such file or directory",452 run("/bin/echo abc > __test/_bogus/xyz"))453 def test_run_rebuild_path_cache_to_discover_new_file(self):454 os.makedirs("__test")455 writeFile("__test/x", "#!/bin/sh\n/bin/echo __test/x")456 self.assertIn(" x: command not found\n__test/x",457 run("PATH=\"/bin:`pwd`/__test\"; x; chmod +x __test/x; x"))458 def test_run_rebuild_path_cache_path_changed(self):459 os.makedirs("__test")460 writeFile("__test/x", "#!/bin/sh\n/bin/echo __test/x")461 os.chmod("__test/x", 0o777)462 self.assertIn(" x: command not found\n__test/x",463 run("x; PATH=\"/bin:`pwd`/__test\"; x"))464 def test_run_no_such_executable(self):465 os.makedirs("__test")466 self.assertIn("No such file or directory", run("__test/bogus foo bar"))467 self.assertIn("No such file or directory",468 run("__test/bogus foo bar | /bin/echo foo"))469 self.assertNotIn("No such file or directory",470 run("__test/bogus foo bar |& /bin/echo foo"))471 self.assertEqual("",472 run("__test/bogus foo bar |& /bin/cat > __test/out"))473 self.assertIn("No such file or directory",474 readFile("__test/out"))475 def test_run_set_status_variable(self):476 os.makedirs("__test")477 self.assertEqual("0\n", run("/bin/true; /bin/echo $?"))478 self.assertEqual("44\n", run("/bin/bash -c \"exit 44\"; /bin/echo $?"))479class TestVariables(unittest.TestCase):480 def test_set(self):481 self.assertEqual("50 100\n", run("x=99; y=100; x=50; /bin/echo $x $y"))482 self.assertIn("x=100",483 run("x=99; export x; /bin/echo foo; x=100; /usr/bin/printenv"))484 def test_setFromEnviron(self):485 self.assertNotEqual("\n", run("/bin/echo $HOME"))486 self.assertNotEqual("\n", run("/bin/echo $SHELL"))487 self.assertIn("SHELL=xyzzy", run("SHELL=xyzzy; /usr/bin/printenv"))488 def test_set(self):489 self.assertEqual(".99. ..\n", run("x=99; /bin/echo .$x. .$y."))490 def test_getEnviron(self):491 result = run("x=99; export x; y=100; /usr/bin/printenv")492 self.assertIn("x=99", result)493 self.assertNotIn("y=", result)494 self.assertIn("HOME=", result)495 self.assertIn("SHELL=", result)496 def test_unset(self):497 self.assertEqual("~99~\n~~\n",498 run("x=99; /bin/echo ~$x~; unset x; /bin/echo ~$x~"))499 result = run("x=99; export x; echo x:$x; unset x; /usr/bin/printenv")500 self.assertIn("x:99", result);501 self.assertNotIn("x=99", result);502 def test_markExported(self):503 self.assertNotIn("x=99", run("x=99; /usr/bin/printenv"))504 self.assertIn("x=99", run("x=99; /bin/echo foo bar; export x; "505 "/usr/bin/printenv"))506if __name__ == '__main__':...

Full Screen

Full Screen

test_xmlpickle.py

Source:test_xmlpickle.py Github

copy

Full Screen

...17import pickle18import unittest19from zope.xmlpickle import dumps, loads20class Test(unittest.TestCase):21 def __test(self, v, expected_xml=None):22 xml = dumps(v)23 if expected_xml:24 self.assertEqual(xml, expected_xml)25 newv = loads(xml)26 self.assertEqual(newv, v)27 def test_float(self):28 for v in (29 -2000000000.0, -70000.0, -30000.0, -3000.0, -100.0, -99.0, -1.0,30 0.0, 1.0, 99.0, 100.0, 3000.0, 30000.0, 70000.0, 2000000000.0,31 -1.2345678901234567e+308,32 -1.2345678901234567e+30,33 -12345678901234567.,34 -1234567890123456.7,35 -123456789012345.67,36 -12345678901234.567,37 -1234567890123.4567,38 -123456789012.34567,39 -12345678901.234567,40 -1234567890.1234567,41 -123456789.01234567,42 -12345678.901234567,43 -1234567.8901234567,44 -123456.78901234567,45 -12345.678901234567,46 -1234.5678901234567,47 -123.45678901234567,48 -12.345678901234567,49 -1.2345678901234567,50 -.12345678901234567,51 1.2345678901234567e+308,52 1.2345678901234567e+30,53 12345678901234567.,54 1234567890123456.7,55 123456789012345.67,56 12345678901234.567,57 1234567890123.4567,58 123456789012.34567,59 12345678901.234567,60 1234567890.1234567,61 123456789.01234567,62 12345678.901234567,63 1234567.8901234567,64 123456.78901234567,65 12345.678901234567,66 1234.5678901234567,67 123.45678901234567,68 12.345678901234567,69 1.2345678901234567,70 .12345678901234567,71 ):72 self.__test(v)73 def test_int(self):74 for v in (-2000000000, -70000, -30000, -3000, -100, -99, -1,75 0, 1, 99, 100, 3000, 30000, 70000, 2000000000):76 self.__test(v)77 def test_long(self):78 for v in (-2000000000l, -70000l, -30000l, -3000l, -100l, -99l, -1l,79 0l, 1l, 99l, 100l, 3000l, 30000l, 70000l, 2000000000l):80 self.__test(v)81 for i in range(10, 3000, 200):82 v = 30l**i83 self.__test(v)84 self.__test(-v)85 def test_None(self):86 self.__test(None,87 '<?xml version="1.0" encoding="utf-8" ?>\n'88 '<pickle> <none/> </pickle>\n'89 )90 def test_True(self):91 self.__test(True)92 self.assertEqual(loads('<pickle><true/></pickle>'), True)93 def test_False(self):94 self.__test(False)95 self.assertEqual(loads('<pickle><false/></pickle>'), False)96 def test_reduce(self):97 v = DictSub()98 v['spam'] = 1.2399 v['eggs'] = 0100 v.name = 'v'101 self.__test(v)102 def test_string(self):103 self.__test('hello\n\tworld',104 '<?xml version="1.0" encoding="utf-8" ?>\n'105 '<pickle> <string>hello\n'106 '\tworld</string> </pickle>\n'107 )108 self.__test('hello\r\nworld',109 '<?xml version="1.0" encoding="utf-8" ?>\n'110 '<pickle> <string>hello&#x0d;\nworld</string> </pickle>\n'111 )112 self.__test('hello\0\nworld',113 '<?xml version="1.0" encoding="utf-8" ?>\n'114 '<pickle> '115 '<string encoding="base64">aGVsbG8ACndvcmxk</string>'116 ' </pickle>\n'117 )118 v = ''.join(map(chr, range(256)))119 self.__test(v[:-2])120 self.__test(v * 10)121 self.__test(v * 400)122 self.__test(v * 4000)123 def test_unicode(self):124 self.__test(125 u'hello\n\tworld',126 '<?xml version="1.0" encoding="utf-8" ?>\n'127 '<pickle> <unicode>hello\n'128 '\tworld</unicode> </pickle>\n'129 )130 self.__test(131 u'hello\r\nworld',132 '<?xml version="1.0" encoding="utf-8" ?>\n'133 '<pickle> <unicode>hello&#x0d;\nworld</unicode> </pickle>\n'134 )135 self.__test(136 u'hello\0\nworld\n',137 '<?xml version="1.0" encoding="utf-8" ?>\n<pickle> '138 '<unicode encoding="base64">aGVsbG8ACndvcmxkCg==</unicode>'139 ' </pickle>\n'140 )141 self.__test(142 u'hello\ufffe\nworld\n',143 '<?xml version="1.0" encoding="utf-8" ?>\n<pickle> '144 '<unicode encoding="base64">aGVsbG/vv74Kd29ybGQK</unicode>'145 ' </pickle>\n'146 )147 self.__test(148 u'Hello\ud7ff\nworld\n',149 '<?xml version="1.0" encoding="utf-8" ?>\n<pickle> '150 '<unicode>Hello\xed\x9f\xbf\nworld\n</unicode>'151 ' </pickle>\n'152 )153 def test_simple_object(self):154 expect = """\155<?xml version="1.0" encoding="utf-8" ?>156<pickle>157 <%sobject>158 <klass>159 <global name="%s" module="%s"/>160 </klass>161 <attributes>162 <attribute name="eggs">163 <int>2</int>164 </attribute>165 <attribute name="spam">166 <int>1</int>167 </attribute>168 </attributes>169 </%sobject>170</pickle>171"""172 self.__test(Simple(1,2),173 expect % ('classic_', 'Simple', __name__, 'classic_'))174 self.__test(newSimple(1,2),175 expect % ('', 'newSimple', __name__, ''))176 self.__test(newSimple(1,2))177 def test_object_w_funny_state(self):178 o = Simple(1,2)179 o.__dict__[(5,6,7)] = None180 self.__test(o)181 o = newSimple(1,2)182 o.__dict__[(5,6,7)] = None183 self.__test(o)184 def test_object_w_initial_args(self):185 o = WInitial([1,2,3], None)186 o.foo = 'bar'187 self.__test(o)188 o = newWInitial([1,2,3], None)189 o.foo = 'bar'190 self.__test(o)191 def test_dict(self):192 self.__test({})193 self.__test({1.23: u'foo', 'spam': [123]})194 d = {}195 for i in range(1000):196 d[i] = str(i*i)197 self.__test(d)198 def test_complex(self):199 self.__test(complex(1,2))200 def test_cycles(self):201 # Python 2.4 does not support recursive comparisons to the202 # same extent as Python 2.3, so we test these recursive203 # structures by comparing the standard pickles of the original204 # and pickled/unpickled copy. This works for things like205 # lists and tuples, but could easily break for dictionaries.206 l = [1, 2 , 3]207 l.append(l)208 xml = dumps(l)209 newl = loads(xml)210 p1 = pickle.dumps(l)211 p2 = pickle.dumps(newl)212 self.assertEqual(p1, p2)213 t = l, l214 l.append(t)215 xml = dumps(l)216 newl = loads(xml)217 p1 = pickle.dumps(l)218 p2 = pickle.dumps(newl)219 self.assertEqual(p1, p2)220 def test_list(self):221 self.__test([])222 self.__test([1,2,3])223 self.__test(range(1000))224 def test_tuple(self):225 self.__test(())226 self.__test((1,2,3))227class Simple:228 """This class is expected to be a classic class."""229 def __init__(self, *a):230 self.spam, self.eggs = a231 def __eq__(self, other):232 return self.__dict__ == other.__dict__233 def __ne__(self, other):234 return not self.__eq__(other)235 def __repr__(self):236 return '%s %r' % (self.__class__.__name__, self.__dict__)237class WInitial(Simple):238 def __getinitargs__(self):239 return self.spam, self.eggs240class newSimple(Simple, object):...

Full Screen

Full Screen

Executable.js

Source:Executable.js Github

copy

Full Screen

...58 command.dispose();59 widget.destroy();60 },61 testToggleButton: function() {62 this.__test(new qx.ui.form.ToggleButton());63 },64 testCheckBox: function() {65 this.__test(new qx.ui.form.CheckBox());66 },67 testButton: function() {68 this.__test(new qx.ui.form.Button());69 },70 testRepeatButton: function() {71 this.__test(new qx.ui.form.RepeatButton());72 },73 testMenuButton: function() {74 this.__test(new qx.ui.form.MenuButton());75 },76 testRadioButton: function() {77 this.__test(new qx.ui.form.RadioButton());78 },79 testToolbarButton: function() {80 this.__test(new qx.ui.toolbar.Button());81 },82 testSplitButton: function() {83 this.__test(new qx.ui.toolbar.SplitButton());84 },85 testMenuCheckBox: function() {86 this.__test(new qx.ui.menu.CheckBox());87 },88 testMenuRadioButton: function() {89 this.__test(new qx.ui.menu.RadioButton());90 },91 testButtonInMenu: function() {92 this.__test(new qx.ui.menu.Button());93 },94 testCheckGroupBox: function() {95 this.__test(new qx.ui.groupbox.CheckGroupBox());96 },97 testRadioGroupBox: function() {98 this.__test(new qx.ui.groupbox.RadioGroupBox());99 },100 testDateChooser: function() {101 this.__test(new qx.ui.control.DateChooser());102 }103 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('storybook-test-runner').__test;2test('test1', () => {3 expect(true).toBe(true);4});5test('test2', () => {6 expect(1).toBe(1);7});8test('test3', () => {9 expect('test').toBe('test');10});11test('test4', () => {12 expect(true).toBe(false);13});14test('test5', () => {15 expect(1).toBe(2);16});17test('test6', () => {18 expect('test').toBe('test1');19});20test('test7', () => {21 expect(true).toBe(true);22});23test('test8', () => {24 expect(1).toBe(1);25});26test('test9', () => {27 expect('test').toBe('test');28});29test('test10', () => {30 expect(true).toBe(false);31});32test('test11', () => {33 expect(1).toBe(2);34});35test('test12', () => {36 expect('test').toBe('test1');37});38test('test13', () => {39 expect(true).toBe(true);40});41test('test14', () => {42 expect(1).toBe(1);43});44test('test15', () => {45 expect('test').toBe('test');46});47test('test16', () => {48 expect(true).toBe(false);49});50test('test17', () => {51 expect(1).toBe(2);52});53test('test18', () => {54 expect('test').toBe('test1');55});56test('test19', () => {57 expect(true).toBe(true);58});59test('test20', () => {60 expect(1).toBe(1);61});62test('test21', () => {63 expect('test').toBe('test');64});65test('test22', () => {66 expect(true).toBe(false);67});68test('test23', () => {69 expect(1).toBe(2);70});71test('test24', () => {72 expect('test').toBe('test1');73});74test('test25', () => {75 expect(true).toBe(true);76});77test('test26', () => {78 expect(1).toBe(1);79});80test('test27', () => {81 expect('test').toBe('test');82});83test('test28', () => {84 expect(true).toBe(false);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { __test } = require('storybook-test-runner')2const { stories } = require('./stories')3__test(stories)4const { storiesOf } = require('@storybook/react')5const { __test } = require('storybook-test-runner')6const stories = storiesOf('Test', module)7stories.add('simple', () => <div>simple</div>)8stories.add('complex', () => <div>complex</div>)9__test(stories)10const { storiesOf } = require('@storybook/react')11const { __test } = require('storybook-test-runner')12const stories = storiesOf('Test', module)13stories.add('simple', () => <div>simple</div>)14stories.add('complex', () => <div>complex</div>)15__test(stories)16const { storiesOf } = require('@storybook/react')17const { __test } = require('storybook-test-runner')18const stories = storiesOf('Test', module)19stories.add('simple', () => <div>simple</div>)20stories.add('complex', () => <div>complex</div>)21__test(stories)22const { storiesOf } = require('@storybook/react')23const { __test } = require('storybook-test-runner')24const stories = storiesOf('Test', module)25stories.add('simple', () => <div>simple</div>)26stories.add('complex', () => <div>complex</div>)27__test(stories)28const { storiesOf } = require('@storybook/react')29const { __test } = require('storybook-test-runner')30const stories = storiesOf('Test', module)31stories.add('simple', () => <div>simple</div>)32stories.add('complex', () => <div>complex</div>)33__test(stories)34const { storiesOf } = require('@storybook/react')35const { __test } = require('storybook-test-runner')

Full Screen

Using AI Code Generation

copy

Full Screen

1const { __test } = require('storybook-test-runner');2__test('test', () => {3});4const { test } = require('storybook-test-runner');5test('test', () => {6});

Full Screen

Using AI Code Generation

copy

Full Screen

1const test = require('storybook-test-runner').__test;2test('MyComponent', () => {3});4const test = require('storybook-test-runner').__test;5test('MyComponent', () => {6});7const test = require('storybook-test-runner').__test;8test('MyComponent', () => {9});10const test = require('storybook-test-runner').__test;11test('MyComponent', () => {12});13const test = require('storybook-test-runner').__test;14test('MyComponent', () => {15});16const test = require('storybook-test-runner').__test;17test('MyComponent', () => {18});19const test = require('storybook-test-runner').__test;20test('MyComponent', () => {21});22const test = require('storybook-test-runner').__test;23test('MyComponent', () => {24});25const test = require('storybook-test-runner').__test;26test('MyComponent', () => {27});28const test = require('storybook-test-runner').__test;29test('MyComponent', () => {30});31const test = require('storybook-test-runner').__test;32test('MyComponent', () => {33});

Full Screen

Using AI Code Generation

copy

Full Screen

1const { __test } = require('@storybook/test-runner');2__test({3 testFrameworkConfig: {4 },5});6import '@testing-library/jest-dom';7import 'jest-styled-components';8import 'jest-axe/extend-expect';9import 'jest-canvas-mock';10import { configure } from 'enzyme';11import Adapter from 'enzyme-adapter-react-16';12configure({ adapter: new Adapter() });13"scripts": {14 },15module.exports = {16 core: {17 },18};19import { addDecorator } from '@storybook/react';20import { withA11y } from '@storybook/addon-a11y';21import { withTests } from '@storybook/addon-jest';22import { withInfo } from '@storybook/addon-info';23import { withNextRouter } from 'storybook-addon-next-router';24import results from '../.jest-test-results.json';25addDecorator(withA11y);26addDecorator(withInfo);27addDecorator(withNextRouter());28addDecorator(29 withTests({30 filesExt: '((\\.specs?)|(\\.tests?))?(\\.ts|\\.tsx)?$',31 })32);33const path = require('path');34const Dotenv = require('dotenv-webpack');35module.exports = async ({ config }) => {36 config.module.rules.push({37 test: /\.(ts|tsx)$/,

Full Screen

Using AI Code Generation

copy

Full Screen

1import { storybookTestRunner } from 'storybook-test-runner';2storybookTestRunner({3 __test: (test, story) => {4 test('test description', async () => {5 await story.click('button');6 });7 },8});9import { storybookTestRunner } from 'storybook-test-runner';10storybookTestRunner({11 __test: (test, story) => {12 test('test description', async () => {13 await story.click('button');14 });15 },16});17import { storybookTestRunner } from 'storybook-test-runner';18storybookTestRunner({19 __test: (test, story) => {20 test('test description', async () => {21 await story.click('button');22 });23 },24});25import { storybookTestRunner } from 'storybook-test-runner';26storybookTestRunner({27 __test: (test, story) => {28 test('test description', async () => {29 await story.click('button');30 });31 },32});33import { storybookTestRunner } from 'storybook-test-runner';34storybookTestRunner({35 __test: (test, story) => {36 test('test description', async () => {37 await story.click('button');38 });39 },40});41import { storybookTestRunner } from 'storybook-test-runner';42storybookTestRunner({43 __test: (test, story) => {44 test('test description', async () => {45 await story.click('button');46 });47 },48});49import { storybookTestRunner }

Full Screen

Using AI Code Generation

copy

Full Screen

1import { __test } from 'storybook-test-runner';2const story = {3};4export default story;5export const test = __test(story);6import { test } from './test';7test();8import testStorybook from 'storybook-tester';9import story from './storybook';10testStorybook(story);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-test-runner automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful