コード例 #1
0
ファイル: context.py プロジェクト: server-mechanic/mechanic
    def __init__(self, args, logger=None, mode=None):
        self.commandLine = CommandLine(args)
        if mode is not None:
            self.commandLine.mode = mode
        if logger is None:
            logging.basicConfig(
                format=
                "%(asctime)-15s %(levelname)s [%(filename)s:%(lineno)d-%(thread)d] %(message)s"
            )
            logger = logging.getLogger()
            logger.setLevel(logging.INFO)
        self.logger = logger

        self.mode = self.__detectMode(self.commandLine.mode)
        self.config = self.__loadConfig()
        self.inventory = Inventory(logger, self.config)
        self.commands = {
            'migrate': MigrateCommand(self),
            'list-migrations': ListMigrationsCommand(self),
            'version': VersionCommand(self),
            'help': HelpCommand(self)
        }
        self.defaultCommand = HelpCommand(self)
コード例 #2
0
 def testOverrfideLogFile(self):
     self.args = CommandLine(["-o", "main.log_file=anotherLogFile"])
     self.assertEquals("anotherLogFile",
                       self.args.overrideOpts["main.log_file"])
コード例 #3
0
class CommandLineTest(unittest.TestCase):
    def setUp(self):
        self.args = CommandLine([
            "-v", "-o", "foo=bar", "--global-flag=global_val", "sub",
            "--sub-flag=sub_val", "--", "followUp", "followUpFlag"
        ])

    def __givenArgsOf(self, args):
        self.args = CommandLine(args)

    def testOverrideRecognized(self):
        self.assertEqual(self.args.overrideOpts["foo"], "bar")

    def testCommandNameRecognized(self):
        self.assertEqual(self.args.commandName, "sub")

    def testVerboseTrueRecognized(self):
        self.assertTrue(self.args.verbose)

    def testDuplicateCommandDetected(self):
        with self.assertRaises(Exception) as context:
            CommandLine(["help", "version"])
        self.assertTrue(
            'Only single command allowed.' in str(context.exception))

    def testMissingArgumentToDashM(self):
        with self.assertRaises(Exception) as context:
            CommandLine(["-m"])
        self.assertTrue('-m requires' in str(context.exception))

    def testInvalidArgumentToDashM(self):
        with self.assertRaises(Exception) as context:
            CommandLine(["-m", "invalid"])
        self.assertTrue('-m requires' in str(context.exception))

    def testUserMode(self):
        self.args = CommandLine(["-m", "user"])
        self.assertEquals("USER", self.args.mode)

    def testSystemMode(self):
        self.args = CommandLine(["-m", "system"])
        self.assertEquals("SYSTEM", self.args.mode)

    def testMissingConfigFileArg(self):
        with self.assertRaises(Exception) as context:
            CommandLine(["-c"])
        self.assertTrue('-c requires' in str(context.exception))

    def testConfigFile(self):
        self.args = CommandLine(["-c", "anotherConfigFile"])
        self.assertEquals("anotherConfigFile", self.args.configFile)

    def testMissingArgumentToDashO(self):
        with self.assertRaises(Exception) as context:
            CommandLine(["-o"])
        self.assertTrue('-o requires key=value' in str(context.exception))

    def testInvalidArgumentToDashO(self):
        with self.assertRaises(Exception) as context:
            CommandLine(["-o", "key"])
        self.assertTrue('-o requires key=value' in str(context.exception))

    def testOverrfideLogFile(self):
        self.args = CommandLine(["-o", "main.log_file=anotherLogFile"])
        self.assertEquals("anotherLogFile",
                          self.args.overrideOpts["main.log_file"])

    def testVerboseFalseRecognized(self):
        self.__givenArgsOf(["sub"])
        self.assertFalse(self.args.verbose)

    def testSubFlagRecognized(self):
        self.assertTrue(self.args.hasSubOpt('sub-flag'))
        self.assertEquals(self.args.subOpts['sub-flag'], 'sub_val')
        self.assertEquals(self.args.getSubOpt('sub-flag'), 'sub_val')
        self.assertEquals(self.args.getSubOpt('absent-sub-flag'), None)
        self.assertEquals(self.args.getSubOpt('absent-sub-flag', 'absent_val'),
                          'absent_val')

    def testGlobalFlagRecognized(self):
        self.assertTrue(self.args.hasGlobalOpt('global-flag'))
        self.assertEquals(self.args.globalOpts['global-flag'], 'global_val')

    def testFollowUpRecognized(self):
        self.assertEquals(self.args.followUpCommand,
                          ["followUp", "followUpFlag"])

    def testFollowUpAbsentWhenNonePassedIn(self):
        self.__givenArgsOf(["sub"])
        self.assertEquals(self.args.followUpCommand, None)

    def testFollowUpAbsentWhenEmptyPassedIn(self):
        self.__givenArgsOf(["sub", "--"])
        self.assertEquals(self.args.followUpCommand, None)

    def tearDown(self):
        self.args = None
コード例 #4
0
 def testConfigFile(self):
     self.args = CommandLine(["-c", "anotherConfigFile"])
     self.assertEquals("anotherConfigFile", self.args.configFile)
コード例 #5
0
 def testInvalidArgumentToDashO(self):
     with self.assertRaises(Exception) as context:
         CommandLine(["-o", "key"])
     self.assertTrue('-o requires key=value' in str(context.exception))
コード例 #6
0
 def testSystemMode(self):
     self.args = CommandLine(["-m", "system"])
     self.assertEquals("SYSTEM", self.args.mode)
コード例 #7
0
 def testMissingConfigFileArg(self):
     with self.assertRaises(Exception) as context:
         CommandLine(["-c"])
     self.assertTrue('-c requires' in str(context.exception))
コード例 #8
0
 def testInvalidArgumentToDashM(self):
     with self.assertRaises(Exception) as context:
         CommandLine(["-m", "invalid"])
     self.assertTrue('-m requires' in str(context.exception))
コード例 #9
0
 def testUserMode(self):
     self.args = CommandLine(["-m", "user"])
     self.assertEquals("USER", self.args.mode)
コード例 #10
0
 def testDuplicateCommandDetected(self):
     with self.assertRaises(Exception) as context:
         CommandLine(["help", "version"])
     self.assertTrue(
         'Only single command allowed.' in str(context.exception))
コード例 #11
0
 def __givenArgsOf(self, args):
     self.args = CommandLine(args)
コード例 #12
0
 def setUp(self):
     self.args = CommandLine([
         "-v", "-o", "foo=bar", "--global-flag=global_val", "sub",
         "--sub-flag=sub_val", "--", "followUp", "followUpFlag"
     ])
コード例 #13
0
 def testRunWithInvalidOrderBy(self):
     args = CommandLine(["list-migrations", "--order-by=invalid"])
     with self.assertRaises(MechanicException) as context:
         self.command.run(args)
     self.assertTrue('invalid' in str(context.exception))
コード例 #14
0
 def testRunWithNoMigrations(self):
     args = CommandLine([])
     self.command.run(args)