def test_is_current(self):
        """Test the is_current method. Depends on correct status file
        operation.
        """
        filename = incremental.Incremental.get_stat_file_name()
        try:
            os.unlink(filename)
        except OSError:
            pass
        inc = incremental.Incremental(7)
        self.assertTrue(inc.is_current(0))
        self.assertTrue(inc.is_current(6))
        self.assertTrue(inc.is_current(7))
        self.assertFalse(inc.is_current(8))
        inc = incremental.Incremental(10)
        self.assertFalse(inc.is_current(7))
        self.assertTrue(inc.is_current(8))
        self.assertTrue(inc.is_current(9))
        self.assertTrue(inc.is_current(10))
        self.assertFalse(inc.is_current(11))

        # also test a non-integer
        action = mock.Mock()
        action.__int__ = mock.Mock()         # magic method needs to be
                                             # explicitly assigned
        action.__int__.return_value = 10
        self.assertTrue(inc.is_current(action))
        action.__int__.return_value = 11
        self.assertFalse(inc.is_current(action))
        os.unlink(filename)
 def test_status_file(self):
     """Test that the last increment is kept correctly
     black box testing as far as file contents is concerned
     """
     filename = incremental.Incremental.get_stat_file_name()
     try:
         os.unlink(filename)
     except OSError:
         pass
     inc = incremental.Incremental(7)
     self.assertEqual(-1, inc.lastincr)
     inc = incremental.Incremental(8)
     self.assertEqual(7, inc.lastincr)
     inc = incremental.Incremental(8)
     # twice the same is not an increment!
     self.assertEqual(-1, inc.lastincr)
     os.unlink(filename)
Ejemplo n.º 3
0
 def run_from_cmd(args):
     """Parse the command line (see module __doc__)
        return 2 on syntax error
        Python will return 1 if some exception occurs. So we don't
        do anything else but basic syntax checking here, the rest of the
        code will throw an exception if the the parameters are not good.
     """
     # TODO: in order to be testable this should really be it's own
     # module so we can use a mock Mcfg
     Mcfg.set_log_level()
     usage = globals()["__doc__"]
     result = 0
     if not len(args) in (4, 5):
         result = 2
     elif "run".startswith(args[1]):
         operation = RUN
         if len(args) != 5:
             result = 2
         else:
             try:
                 run_incr = int(args[4])
             except ValueError:
                 result = 2
     elif "verify".startswith(args[1]):
         operation = VERIFY
     elif "switchuser".startswith(args[1]):
         operation = SWITCHUSER
     else:
         result = 2
     if result != 2:
         if operation == RUN:
             master = Mcfg(args[2], args[3])
             incr = incremental.Incremental(run_incr)
             master.run_editors(incr)
         elif operation == SWITCHUSER:
             incremental.Incremental.copy_stat_file(args[2], args[3])
         else:
             raise NotImplementedError, "verify"
     else:
         print >> sys.stderr, usage
     return result
 def test_construct_and_convert(self):
     """Just construct it and convert to int"""
     inc = incremental.Incremental(7)
     self.assertEqual(7, int(inc))
     os.unlink(incremental.Incremental.get_stat_file_name())