def testFixture2(self): def MyFix(): return 'This is rubles.' def Cmd(console_, rubles_): console_.Print(rubles_) app = App('test', color=True, buffered_console=True) app.Fixture(MyFix, name='rubles') app.Add(Cmd) self._TestMain(app, 'cmd', 'This is rubles.\n')
def testFixture1(self): def MyFix(): return 'This is a custom fixture' def Cmd(console_, my_fix_): console_.Print(my_fix_) app = App('test', color=True, buffered_console=True) app.Fixture(MyFix) app.Add(Cmd) self._TestMain(app, 'cmd', 'This is a custom fixture\n')
def testBoolArgTrue(self): # We cannot have a command with boolean default True app = App('test', color=False, buffered_console=True) def Command(console_, option=True): console_.Print(option) with pytest.raises(RuntimeError) as e: app.Add(Command) assert str(e.value) == \ 'Clikit commands are not allowed to have boolean parameters that default to True.'
def testPositionalArgs(self): ''' >test command alpha bravo alpha..bravo ''' app = App('test', color=False, buffered_console=True) def Command(console_, first, second): console_.Print('%s..%s' % (first, second)) app.Add(Command) app.TestScript(inspect.getdoc(self.testPositionalArgs))
def testExecuteCommand(self): def Cmd(console_, subject='World'): console_.Print('Hello, %s!' % subject) app = App('test', color=False, buffered_console=True) app.Add(Cmd) retcode, output = app.ExecuteCommand('cmd') assert retcode == app.RETCODE_OK assert output == 'Hello, World!\n' retcode, output = app.ExecuteCommand('cmd', 'Alpha') assert retcode == app.RETCODE_OK assert output == 'Hello, Alpha!\n'
def testBoolArgFalse(self): ''' >test command False >test command --option True ''' app = App('test', color=False, buffered_console=True) def Command(console_, option=False): console_.Print(option) app.Add(Command) app.TestScript(inspect.getdoc(self.testBoolArgFalse))
def testCommandDecorator(self): app = App('test', color=False, buffered_console=True) @app def Alpha(console_): console_.Print('Alpha') @app() def Bravo(console_): console_.Print('Bravo') assert app.ExecuteCommand('alpha') == (app.RETCODE_OK, 'Alpha\n') assert app.ExecuteCommand('bravo') == (app.RETCODE_OK, 'Bravo\n')
def testOptionUnderscore(self): ''' >test command my_option = default >test command --my-option=custom my_option = custom ''' app = App('test', color=False, buffered_console=True) def Command(console_, my_option='default'): console_.Print('my_option = ' + my_option) app.Add(Command) app.TestScript(inspect.getdoc(self.testOptionUnderscore))
def testOptionArgs(self): ''' >test command 1..2 >test command --first=alpha --second=bravo alpha..bravo ''' app = App('test', color=False, buffered_console=True) def Command(console_, first='1', second='2'): console_.Print('%s..%s' % (first, second)) app.Add(Command) app.TestScript(inspect.getdoc(self.testOptionArgs))
def testPositionalArgsWithDefaults(self): ''' >test hello NOTHING >test hello something something ''' app = App('test', color=False, buffered_console=True) def Hello(console_, message=App.DEFAULT('NOTHING')): console_.Print(message) app.Add(Hello) app.TestScript(inspect.getdoc(self.testPositionalArgsWithDefaults))
def testSysArgv(self): def Case(console_, argv_, first, second): console_.Print('%s..%s' % (first, second)) console_.Print(argv_) app = App('test', color=False, buffered_console=True) app.Add(Case) old_sys_argv = sys.argv sys.argv = [sys.argv[0], 'case', 'alpha', 'bravo'] try: app.Main() assert app.console.GetOutput( ) == "alpha..bravo\n['case', 'alpha', 'bravo']\n" finally: sys.argv = old_sys_argv
def testUnknownOptionArgs(self): app = App('test', color=False, buffered_console=True) def Command(console_): console_.Print('hello') app.Add(Command) app.TestScript( Dedent(''' >test command --foo --bar ERROR: Unrecognized arguments: --foo --bar (no description) Usage: command\s\s Parameters: Options: '''.replace('\s', ' ')))
def testColor(self): app = App('test', color=True, buffered_console=True) assert app.console.color == True def Case(): ''' This is Case. ''' app.Add(Case) self._TestMain( app, '', Dedent(''' Usage: test <subcommand> [options] Commands: %(teal)scase%(reset)s This is Case. ''' % Console.COLOR_CODES))
def testFixtureDecorator(self): app = App('test', color=False, buffered_console=True) @app.Fixture def Alpha(): return 'alpha' @app.Fixture() def Bravo(): return 'bravo' def Command(console_, alpha_, bravo_): console_.Print('The names are: %(alpha_)s and %(bravo_)s.' % locals()) app.Add(Command) self._TestMain( app, 'command', Dedent(''' The names are: alpha and bravo. '''))
def testConf(self, tmpdir): ''' Tests the configuration plugin (ConfPlugin) ''' conf_filename = tmpdir.join('ConfigurationCmd.conf') app = App('test', color=False, conf_defaults={'group': { 'value': 'ALPHA', }}, conf_filename=str(conf_filename), buffered_console=True) def ConfigurationCmd(console_, conf_): ''' Test Set/Get methods from configuration object. ''' console_.Print('conf_.filename: %s' % conf_.filename) console_.Print('group.value: %s' % conf_.Get('group', 'value')) conf_.Set('group', 'value', 'BRAVO') console_.Print('group.value: %s' % conf_.Get('group', 'value')) assert not conf_filename.check(file=1) conf_.Save() assert conf_filename.check(file=1) app.Add(ConfigurationCmd) self._TestMain( app, 'configuration-cmd', 'conf_.filename: %s\ngroup.value: ALPHA\ngroup.value: BRAVO\n' % conf_filename) # Creating an application with an existing configuration file. assert conf_filename.check(file=1) app = App('test', color=False, conf_defaults={'group': { 'value': 'ALPHA', }}, conf_filename=str(conf_filename), buffered_console=True) def Cmd(console_, conf_): console_.Print(conf_.filename) console_.Print(conf_.Get('group', 'value')) app.Add(Cmd) self._TestMain(app, 'cmd', '%s\nBRAVO\n' % conf_filename)
def testUnknownApp(self): app = App('test', color=False, buffered_console=True) with pytest.raises(UnknownApp): app.TestCall('UNKNOWN')
def testHelp(self): def TestCmd(console_, first, second, option=1, option_no=False): ''' This is a test. :param first: This is the first parameter. :param second: This is the second and last parameter. :param option: This must be a number. :param option_no: If set, says nop. ''' app = App('test', color=False, buffered_console=True) app.Add(TestCmd) self._TestMain( app, '', Dedent(''' Usage: test <subcommand> [options] Commands: test-cmd This is a test. ''')) self._TestMain( app, '--help', Dedent(''' Usage: test <subcommand> [options] Commands: test-cmd This is a test. ''')) self._TestMain( app, 'test-cmd --help', Dedent(''' This is a test. Usage: test-cmd <first> <second> [--option=1],[--option-no] Parameters: first This is the first parameter. second This is the second and last parameter. Options: --option This must be a number. [default: 1] --option-no If set, says nop. ''')) self._TestMain( app, 'test-cmd', Dedent(''' ERROR: Too few arguments. This is a test. Usage: test-cmd <first> <second> [--option=1],[--option-no] Parameters: first This is the first parameter. second This is the second and last parameter. Options: --option This must be a number. [default: 1] --option-no If set, says nop. '''), app.RETCODE_ERROR)
def Hello(console_, message=App.DEFAULT('NOTHING')): console_.Print(message)
from clikit.app import App from ben10.filesystem import CopyFile, Cwd, IsDir, DeleteDirectory, CopyFiles, \ CopyDirectory from ben10.foundation.pushpop import PushPop import os import subprocess app = App('fz') FORGE_DIR = os.path.abspath('forge') MFL_DIR = os.path.abspath('forge/fml') MCP_DIR = os.path.abspath('mcp') FORGE_URL = 'https://github.com/MinecraftForge/MinecraftForge.git' def SubVars(x): for i in ('FORGE_DIR', 'MFL_DIR', 'MCP_DIR', 'FORGE_URL'): x = x.replace('$' + i, globals()[i]) return x def Execute(console_, cmd, cwd='.', env={}): with Cwd(cwd): new_env = os.environ.copy() new_env.update(env) subprocess.check_call(cmd, env=new_env) def Patch(console_, filename): filename = filename.replace('$FORGE_DIR', FORGE_DIR) dirname, filename = os.path.split(filename)
def testBufferedConsole(self): app = App('test', color=False, buffered_console=True) assert type(app.console) == BufferedConsole app = App('test', color=False) assert type(app.console) == Console
from __future__ import unicode_literals from clikit.app import App import getpass import socket import sys import json app = App('girctl') DEFAULT_ROOM = '#bos-ama' DEFAULT_HOST = 'localhost' DEFAULT_PORT = 5000 @app def Message(console_, room=DEFAULT_ROOM, username=None, host=DEFAULT_HOST, port=DEFAULT_PORT, *message): ''' Sends a message to slack. :param room: Slack room to send the message. :param username: Sender username for the message. :param host: Gir server url. :param port: Gir server port. :param message: The message to send to slack. ''' _SlackMessage(console_, ' '.join(message), room=room, username=username, host=host, port=port)
def testApp(self): ''' Tests App usage and features. ''' def Case1(console_): ''' A "hello" message from case 1 ''' console_.Print('Hello from case 1') def Case2(console_): ''' A "hello" message from case 2 Additional help for this function is available. ''' console_.Print('Hello from case 2') def Case3(console_): console_.Print('Hello from case 3') def Case4(console_): console_.Print('Hello from case 4 (AKA: four)') app = App('test', color=False, buffered_console=True) app.Add(Case1, alias='cs') app.Add(Case2) case3_cmd = app.Add(Case3, alias=('c3', 'cs3')) app.Add(Case4, name='four') # Test duplicate name with pytest.raises(ValueError): app.Add(case3_cmd.func, alias='cs') # Test commands listing assert app.ListAllCommandNames() == [ 'case1', 'cs', 'case2', 'case3', 'c3', 'cs3', 'four' ] # Tests all commands output self._TestMain(app, 'case1', 'Hello from case 1\n') self._TestMain(app, 'cs', 'Hello from case 1\n') self._TestMain(app, 'case2', 'Hello from case 2\n') self._TestMain(app, 'case3', 'Hello from case 3\n') self._TestMain(app, 'c3', 'Hello from case 3\n') self._TestMain(app, 'cs3', 'Hello from case 3\n') # Tests output when an invalid command is requested self._TestMain( app, 'INVALID', Dedent(''' ERROR: Unknown command 'INVALID' Usage: test <subcommand> [options] Commands: case1, cs A "hello" message from case 1 case2 A "hello" message from case 2 case3, c3, cs3 (no description) four (no description) '''), app.RETCODE_ERROR)