예제 #1
0
    def test_persistent_bool_flag(self):
        """

        Tests that the persistent bool flags of a/the command(s) are working

        Command String: 1.<script name>(root) -v
                        2.<script name>(root) child -v
                        3.<script name>(root) child gchild -v

        """
        def run_func(arg):
            pass

        sys.argv = sys.argv[:1]

        root = pikli.Command(use="root",
                             short="the root command",
                             run=run_func)

        child = pikli.Command(use="child",
                              short="the child command",
                              run=run_func)

        gchild = pikli.Command(use="gchild",
                               short="the grand child command",
                               run=run_func)

        root.persistent_flags().boolp("verbose", "v",
                                      "shows details of the operation")

        child.add_command(gchild)

        root.add_command(child)

        sys.argv += ["-v"]

        root.execute()

        verbose = pikli.get_bool("verbose")

        self.assertTrue(verbose, "Verbose should be true")

        sys.argv.pop()
        sys.argv += ["child", "-v"]

        verbose = pikli.get_bool("verbose")

        self.assertTrue(verbose, "Verbose should be true")

        sys.argv.pop()
        sys.argv += ["gchild", "-v"]

        verbose = pikli.get_bool("verbose")

        self.assertTrue(verbose, "Verbose should be true")
예제 #2
0
    def test_persistent_string_flag(self):
        """

        Tests that the persistent string flags are working

        Command String: 1.<script name>(root) -n Alex
                        2.<script name>(root) child1 --name=Barry
                        3.<script name>(root) child2 -n Kane

        """
        def run_func(arg):
            pass

        sys.argv = sys.argv[:1]

        root = pikli.Command(use="root",
                             short="this is the root command",
                             run=run_func)

        child1 = pikli.Command(use="child1",
                               short="the child1 command",
                               run=run_func)

        child2 = pikli.Command(use="child2",
                               short="the child2 command",
                               run=run_func)

        root.add_command(child1)
        root.add_command(child2)

        root.persistent_flags().stringp("name", "n", "unamed", "the name")

        sys.argv += ["-n", "Alex"]

        root.execute()

        self.assertEqual(pikli.get_str("name"), "Alex",
                         "Should have been Alex")

        sys.argv = sys.argv[:1]
        sys.argv += ["child1", "--name=Barry"]

        root.execute()

        self.assertEqual(pikli.get_str("name"), "Barry",
                         "Should have been Barry")

        sys.argv = sys.argv[:1]
        sys.argv += ["child2", "-n", "Kane"]

        root.execute()

        self.assertEqual(pikli.get_str("name"), "Kane",
                         "Should have been Kane")
예제 #3
0
    def test_string_flags(self):
       """

       Tests that the string flags of a/the command(s) are working

       Command String: <script name>(root) -n Johnny assign --designation="Software Engineer"
                                           -s "active"

       """

       def run_func(arg):
           pass


       sys.argv = sys.argv[:1]

       root = pikli.Command(use = "root" , short = "the root command")

       assign = pikli.Command(use = "assign" , short = "assigns the employee" ,
                             run = run_func)

       root.flags().stringp("name" , "n" , "A man has no name",  "Name of the employee")

       assign.flags().stringp("designation" , "d" , "killer"  , "Designation of the employee")

       assign.flags().stringp("status" , "s" , "inactive" ,"current status of the employee")

       assign.flags().stringp("nothing" , "n" , "nothing" , "flag to test the default value")

       root.add_command(assign)

       sys.argv += ["-n" , "Johnny" , "assign" , "--designation=Software Engineer" , "-s" , "active"]

       root.execute()


       name = pikli.get_str("name")
       designation = pikli.get_str("designation")
       status = pikli.get_str("status")
       nothing = pikli.get_str("nothing")

       want_name = "Johnny"
       want_designation = "Software Engineer"
       want_status = "active"
       want_nothing = "nothing"

       self.assertEqual(name , want_name , "name: Want {} Got {}".format(want_name , name))
       self.assertEqual(designation , want_designation , "designation: Want {} Got {}".format(want_designation , designation))
       self.assertEqual(status , want_status , "status: Want {} Got {}".format(want_status , status))
       self.assertEqual(nothing , want_nothing , "Nothing: Want {} Got {}".format(want_nothing , nothing))
예제 #4
0
    def test_int_flags(self):
        """

        Tests that the int flags of a/the command(s) are working

        Command String: <script name>(root) -y 2019 serve --port=8080 -w 20

        """

        def start_server(arg):
            pass


        sys.argv = sys.argv[:1]

        root = pikli.Command(use = "root" , short = "the root command")

        serve = pikli.Command(use = "serve" , short = "starts the server" ,
                              run = start_server)

        root.flags().intp("year" , "y" , 2018,  "the current year")

        serve.flags().intp("port" , "p" , 8000  , "the port to run the http server")

        serve.flags().intp("worker" , "w" , 10 ,"number of workers")

        serve.flags().intp("nothing" , "n" , 420 , "flag to test the default value")

        root.add_command(serve)

        sys.argv += ["-y" , "2019" , "serve" , "--port=8080" , "-w" , "20"]

        root.execute()


        year = pikli.get_int("year")
        port = pikli.get_int("port")
        worker = pikli.get_int("worker")
        nothing = pikli.get_int("nothing")

        want_year = 2019
        want_port = 8080
        want_worker = 20
        want_nothing = 420

        self.assertEqual(year , want_year , "Year: Want {} Got {}".format(want_year , year))
        self.assertEqual(port , want_port , "Port: Want {} Got {}".format(want_port , port))
        self.assertEqual(worker , want_worker , "Worker: Want {} Got {}".format(want_worker , worker))
        self.assertEqual(nothing , want_nothing , "Nothing: Want {} Got {}".format(want_nothing , nothing))
예제 #5
0
    def test_multilevel_commands(self):
        """

        Tests that commands/sub-commands on multilevel are working

        Command String: 1.<script name>(root) child
                        2.<script name>(root) child gchild

        """
        def run_func(arg):
            TestCommand.worked = True

        sys.argv = sys.argv[:1]

        root = pikli.Command(use="root", short="The root command")

        child = pikli.Command(use="child",
                              short="The child command",
                              run=run_func)

        gchild = pikli.Command(use="gchild",
                               short="The grand child command",
                               run=run_func)

        child.add_command(gchild)

        root.add_command(child)

        sys.argv += ["child"]

        root.execute()

        self.assertTrue(TestCommand.worked, "Should be true")

        TestCommand.worked = False

        sys.argv += ["gchild"]

        root.execute()

        self.assertTrue(TestCommand.worked, "Should be true")

        TestCommand.worked = False
예제 #6
0
    def test_sub_commands(self):
        """

         Tests that the sub commands work with the run function

         Command String: 1.<script name>(root) sub1
                         2.<script name>(root) sub2

        """
        def run_func(arg):
            TestCommand.worked = True

        sys.argv = sys.argv[:1]

        root = pikli.Command(use="root", short="the root command")

        sub1 = pikli.Command(use="sub1",
                             short="the first sub command",
                             run=run_func)

        sub2 = pikli.Command(use="sub2",
                             short="the second sub command",
                             run=run_func)

        root.add_command(sub1, sub2)

        sys.argv += ["sub1"]

        root.execute()

        self.assertTrue(TestCommand.worked, "Should be true")

        TestCommand.worked = False

        sys.argv.pop()

        sys.argv += ["sub2"]

        root.execute()

        self.assertTrue(TestCommand.worked, "Should be true")

        TestCommand.worked = False
예제 #7
0
    def test_args(self):
        """

        Tests that the arguments provided with the commands are properly parsed
        among the flags

        Command String: <script name>(root) John -p 8080 Doe --worker=20 child

        """
        def run_func(arg):
            TestArgs.fname = arg[0]
            TestArgs.lname = arg[1]

        def no_func(arg):
            pass

        sys.argv = sys.argv[:1]

        root = pikli.Command(use="root",
                             short="the root command",
                             run=run_func)
        child = pikli.Command(use="child",
                              short="the child command",
                              run=no_func)

        root.flags().intp("port", "p", 8000, "the port")
        root.flags().intp("worker", "w", 10, "the workers")

        sys.argv += ["John", "-p", "8080", "Doe", "--worker=20", "child"]

        root.execute()

        got = TestArgs.fname + " " + TestArgs.lname

        want = "John Doe"

        self.assertEqual(got, want, "{} should have been {}".format(got, want))

        port = pikli.get_int("port")
        worker = pikli.get_int("worker")

        self.assertEqual(port, 8080, "Should have been 8080")
        self.assertEqual(worker, 20, "Should have been 20")
예제 #8
0
    def test_bool_flags(self):
        """

        Tests that the bool flags of a/the command(s) are working

        Command String: <script name>(root) -v serve --http

        """

        def start_server(arg):
            pass


        sys.argv = sys.argv[:1]

        root = pikli.Command(use = "root" , short = "the root command")

        serve = pikli.Command(use = "serve" , short = "starts the server" ,
                              run = start_server)

        root.flags().boolp("verbose" , "v" , "show details of an operation")

        serve.flags().boolp("http" , "h" , "starts the http server")

        serve.flags().boolp("grpc" , "g" , "starts the gRPC server")

        root.add_command(serve)

        sys.argv += ["-v" , "serve" , "--http"]

        root.execute()


        verbose = pikli.get_bool("verbose")
        http = pikli.get_bool("http")
        grpc = pikli.get_bool("grpc")

        self.assertTrue(verbose , "Verbose Should be true")
        self.assertTrue(http , "http should be true")
        self.assertFalse(grpc , "grpc shoud be false")
예제 #9
0
    def test_persistent_int_flag(self):
        """

        Tests that the persistent int flags are working

        Command String: 1.<script name>(root) serve --port=8080 http
                        2.<script name>(root) serve http -p 3000

        """
        def run_func(arg):
            pass

        sys.argv = sys.argv[:1]

        root = pikli.Command(use="root", short="this is the root command")

        serve = pikli.Command(use="serve", short="starts the server")

        http = pikli.Command(use="http", short="the http server", run=run_func)

        serve.add_command(http)

        serve.persistent_flags().intp("port", "p", 8000,
                                      "the port to run the server on")

        root.add_command(serve)

        sys.argv += ["serve", "--port=8080", "http"]

        root.execute()

        self.assertEqual(pikli.get_int("port"), 8080, "Should have been 8080")

        sys.argv = sys.argv[:1]
        sys.argv += ["serve", "http", "-p", "3000"]

        root.execute()

        self.assertEqual(pikli.get_int("port"), 3000, "Should have been 3000")
예제 #10
0
    def test_root_command(self):
        """

          Tests that a single command(root) works with the run function

          Command string: <script name>(root)

        """
        def run_func(arg):
            TestCommand.worked = True

        sys.argv = sys.argv[:1]

        root = pikli.Command(use="root",
                             short="the root command",
                             run=run_func)

        root.execute()

        self.assertTrue(TestCommand.worked, "Should be true")

        TestCommand.worked = False