def test_del_variable(self): """ Tests the del function on a user-defined variable. """ # create the variable ergo("set x 2") # delete it ergo("del x") # check that it's not still in the namespace self.assertEqual(ergo("print $x"), None)
def test_cd(self): """ Test the cd command. """ original = ergo("pwd") try: os.mkdir("test-cd") except OSError: # directory already exists pass ergo("cd test-cd") self.assertEqual(original + "/test-cd", ergo("pwd"))
def test_pyvim(self): """ Tests the pyvim command. """ try: ergo("pyvim") except Exception as error: if error.args[ 0] == "[ergo]: [pyvim: PlatformError]: Pyvim not supported on this system.": pass else: raise error
def test_py(self): """ Tests the py command. """ # set a variable in the Ergonomica namespace ergo("set l 2") # use it in a Python expression self.assertEqual(ergo("py \"l + 2\""), 4) # delete the variable after use ergo("del l")
def test_cd(self): """ Tests the cd command. """ olddir = os.getcwd() newdir = tempfile.mkdtemp() ergo("cd {}".format(newdir)) self.assertEqual(os.getcwd(), newdir) ergo("cd {}".format(olddir)) self.assertEqual(os.getcwd(), olddir)
def test_read(self): """ Tests the read command. """ open("test-read", "w").write("we are number one") self.assertEqual(ergo("read test-read"), ["we are number one"])
def test_random(self): """ Tests that the random function works properly. """ value = ergo("random") self.assertTrue((0 < value) and (1 > value))
def test_flatten(self): """ Tests that the flatten function works properly. """ self.assertEqual(set(ergo("flatten (list (list 1 2 (list 3)) 4 5)")), {1, 2, 3, 4, 5})
def test_net_globalip(self): """ Tests the net command getting the global IP. """ self.assertEqual(ergo("net ip global"), requests.get("http://ip.42.pl/raw").text)
def test_single_print(self): """ Test printing a single string. """ self.assertEqual(ergo('print "this is an example of a \'string\'"'), "this is an example of a 'string'")
def test_range_start_end_step(self): """ Test a range with start, end, and step specified. """ self.assertCountEqual(ergo("range 1 10 1"), [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
def test_flat_find_file(self): """ Tests the find command trying to find files limited to the current directory (non-recursively). """ makedirstructure() self.assertCountEqual(ergo("find -f file a.*"), ['a', 'a.jpeg']) rmdirstructure()
def test_find_file(self): """ Tests the find command trying to find files. """ makedirstructure() self.assertCountEqual(ergo("find file a.*"), ['./a.jpeg', './b/a.rst']) rmdirstructure()
def test_time(self): """ Test the time function. """ # here we essentially ensure that strftime returns the same self.assertEqual(ergo('time {}'.format(STRFTIME_TEST_STRING)), strftime(STRFTIME_TEST_STRING, gmtime()))
def test_range_start_end_step_undershoot(self): """ Test a range in which (end - start) is not divisible by step. """ self.assertCountEqual( ergo("range 1 10 1.7"), [1.0, 2.7, 4.4, 6.1000000000000005, 7.800000000000001, 9.5])
def test_help(self): """ Test the help command. """ self.assertEqual( ergo("help echo"), "echo [STRING,...] | Prints its input.\n\nVisit https://github.com/ergonomica/ergonomica/wiki for more documentation." )
def test_read(self): """ Tests the read command. """ # create the files for testing with open("test_read_1", "w") as f, open("test_read_2", "w") as g: f.write("a\nb\nc") g.write("1\n2\n3") # assert that they match what Ergonomica reads self.assertEqual(ergo("read test_read_1"), ["a", "b", "c"]) self.assertEqual(ergo("read test_read_2"), ["1", "2", "3"]) # delete them os.remove("test_read_1") os.remove("test_read_2")
def test_mixed_quotes(self): """ "Single quotes between double quotes not parsing correctly" @lschumm https://github.com/ergonomica/ergonomica/issues/17 """ self.assertEqual(ergo("echo \"hello 'world'\""), ["hello 'world'"])
def test_mv(self): """ Tests the mv command. """ with open("test_mv_file", "w") as f: f.write("example content") ergo("mv test_mv_file test_mv_file2") # ensure the second file has the correct content with open("test_mv_file2") as f: self.assertEqual(f.read(), "example content") # ensure that the last file was deleted self.assertFalse(os.path.isfile("test_mv_file")) os.remove("test_mv_file2")
def test_help(self): """ Test the help command. """ self.assertEqual( ergo("help echo"), "echo [STRING,...] {ind:[INT,...]} | " "Prints its input. If ind specified, returns the items of its input with the specified indices.\n\n" "Visit https://github.com/ergonomica/ergonomica/wiki for more documentation." )
def test_net_mac(self): """ Tests the net command getting the MAC address. """ for interface in netifaces.interfaces(): self.assertEqual(ergo("net mac {}".format(interface)), netifaces.ifaddresses(interface)\ [netifaces.AF_LINK][0]['addr'])
def test_cp(self): """ Tests the cp command. """ with open("test_cp_file", "w") as f: f.write("example content") ergo("cp test_cp_file test_cp_file2") # ensure the second file has the correct content self.assertEqual(open("test_cp_file2").read(), "example content") # ensure that the last file was not deleted self.assertEqual(open("test_cp_file").read(), "example content") os.remove("test_cp_file") os.remove("test_cp_file2")
def test_net_localip(self): """ Tests the net command getting the local IP. """ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) self.assertEqual(ergo("net ip local"), s.getsockname()[0]) s.close()
def test_show_c(self): """ Tests `license show c` showing the copyright. """ self.assertEqual( ergo("license show c"), "Ergonomica Copyright (C) 2017 Liam Schumm, Andy Merrill, Dhyan Patel, Pavel Golubev" )
def test_rm(self): """ Tests the rm command. """ with open("test_rm_file", "w") as f: f.write("example content") with open("test_rm_file2", "w") as f: f.write("example content") # test that files were created properly self.assertTrue(os.path.isfile("test_rm_file")) self.assertTrue(os.path.isfile("test_rm_file2")) ergo("rm test_rm_file test_rm_file2") self.assertFalse(os.path.isfile("test_rm_file")) self.assertFalse(os.path.isfile("test_rm_file2"))
def test_hash_add(self): """ Tests that the hash function works properly. """ self.assertEqual(ergo("hash-add 9 2 (hash (list 1 2 3 '4'))"), { 1: 2, 3: '4', 9: 2 })
def test_flat_find_line(self): """ Tests the find command trying to find a line limited to the current directory (non-recursively). """ global UNIQUE_STRING makedirstructure() self.assertCountEqual(ergo("find -f string {}".format(UNIQUE_STRING)), ["a.jpeg: {}".format(UNIQUE_STRING)]) rmdirstructure()
def test_size(self): f = open("test_size", "w+") f.write("some bytes") f.flush() self.assertTrue( str(os.path.getsize("test_size")) in ergo( 'size test_size {unit:"B"}')[0]) mkdir_force("size_dir") mkdir_force("size_dir/inner_dir") f1 = open("size_dir/test_size", "w+") f1.write("some bytes") f1.flush() f2 = open("size_dir/inner_dir/test_size", "w+") f2.write("some other bytes") f2.flush() self.assertTrue( str( os.path.getsize("size_dir/test_size") + os.path.getsize("size_dir/inner_dir/test_size")) in ergo( 'size size_dir {unit:"B"}')[0])
def test_mkdir(self): """ Tests the mkdir command. """ if os.path.isdir("mkdir_test"): os.rmdir("mkdir_test") ergo("mkdir mkdir_test") self.assertTrue(os.path.isdir("mkdir_test")) error_dir_exist = False mkdir_force("mkdir_test2") try: ergo('mkdir mkdir_test2') except: error_dir_exist = True self.assertTrue(error_dir_exist) error_dir_exist_overwrite = False mkdir_force("mkdir_test3") open("mkdir_test3/file1", "w+") # test if folder was overwritten try: ergo('mkdir mkdir_test3 {overwrite:"true"}') except: error_dir_exist_overwrite = True self.assertFalse(error_dir_exist_overwrite) self.assertEqual(os.listdir("mkdir_test3"), [])
def test_list_modules(self): """ Tests the list_modules command. """ # old (confirmed working) implementation. test that any new implementation will match. files = os.listdir( os.path.join(os.path.join(os.path.expanduser("~"), ".ergo"), "packages")) self.assertEqual(ergo('list_modules'), [ f.replace(".py", "") for f in files if f.endswith(".py") and f != "__init__.py" ])