def testValidateClobberUserDeclines_2(self): """Test case where user does not enter the full 'yes' pattern.""" self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(cros_build_lib, 'GetInput') os.path.exists(self._BUILD_ROOT).AndReturn(True) cros_build_lib.GetInput(mox.IgnoreArg()).AndReturn('asdf') cros_build_lib.GetInput(mox.IgnoreArg()).AndReturn('No') self.mox.ReplayAll() self.assertFalse(commands.ValidateClobber(self._BUILD_ROOT)) self.mox.VerifyAll()
def testGetInput(self): """Verify GetInput() basic behavior.""" response = 'Some response' if sys.version_info.major < 3: self.PatchObject(builtins, 'raw_input', return_value=response) self.PatchObject(builtins, 'input', return_value=response) self.assertEqual(response, cros_build_lib.GetInput('prompt'))
def testInferBuildRootPromptNo(self): """Test that a 'no' answer on the prompt halts execution.""" cros_build_lib.GetInput(mox.IgnoreArg()).InAnyOrder().AndReturn('no') self.mox.ReplayAll() self.assertRaises(TestExitedException, self.assertMain, ['--local', 'x86-generic-paladin'])
def testInferBuildRootExists(self): """Test that we don't prompt the user if buildroot already exists.""" cros_build_lib.RunCommandCaptureOutput(['touch', self.external_marker]) os.utime(self.external_marker, None) (cros_build_lib.GetInput(mox.IgnoreArg()).InAnyOrder().AndRaise( TestFailedException())) self.mox.ReplayAll() self.assertMain(['--local', 'x86-generic-paladin'])
def testGetInput(self): self.mox.StubOutWithMock(__builtin__, 'raw_input') prompt = 'Some prompt' response = 'Some response' __builtin__.raw_input(prompt).AndReturn(response) self.mox.ReplayAll() self.assertEquals(response, cros_build_lib.GetInput(prompt)) self.mox.VerifyAll()
def GetThresholdFromUser(self): """Gets threshold that decides good/bad commit. It gives user the measured last known good and bad score's statistics to help user determine a reasonable threshold. If self.auto_threshold is set, instead of prompting user to pick threshold, it uses half of distance between good and bad commit score as threshold. """ # If threshold is assigned, skip it. if self.threshold is not None: return True good_score = self.good_commit_info.score bad_score = self.bad_commit_info.score logging.notice( 'Good commit score mean: %.3f STD: %.3f\n' 'Bad commit score mean: %.3f STD: %.3f', good_score.mean, good_score.std, bad_score.mean, bad_score.std) if self.auto_threshold: splitter = (good_score.mean + bad_score.mean) / 2.0 self.threshold = abs(good_score.mean - splitter) logging.notice('Automatically pick threshold: %.3f', self.threshold) return True ref_score_min = min(good_score.mean, bad_score.mean) ref_score_max = max(good_score.mean, bad_score.mean) success = False retry = 3 for _ in range(retry): try: splitter = float( cros_build_lib.GetInput( 'Please give a threshold that tell apart good and bad commit ' '(within range [%.3f, %.3f]: ' % (ref_score_min, ref_score_max))) except ValueError: logging.error('Threshold should be a floating number.') continue if ref_score_min <= splitter <= ref_score_max: self.threshold = abs(good_score.mean - splitter) success = True break else: logging.error('Threshold should be within range [%.3f, %.3f]', ref_score_min, ref_score_max) else: logging.error('Wrong threshold input for %d times. Terminate.', retry) return success
def main(argv): parser = GetParser() options = parser.parse_args(argv) logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) # Check that we have no uncommitted files, and that our checkout's HEAD is # contained in a remote branch. This is to ensure that we don't accidentally # run uncommitted migrations. uncommitted_files = git.RunGit(os.getcwd(), ['status', '-s']).output if uncommitted_files: cros_build_lib.Die('You appear to have uncommitted files. Aborting!') remote_branches = git.RunGit(os.getcwd(), ['branch', '-r', '--contains']).output if not remote_branches: cros_build_lib.Die( 'You appear to be on a local branch of chromite. Aborting!') if options.command == MIGRATE: positive_confirmation = 'please modify my database' warn = ( 'This option will apply schema changes to your existing database. ' 'You should not run this against the production database unless ' 'your changes are thoroughly tested, and those tests included ' 'in cidb_integration_test.py (including tests that old data is ' 'sanely migrated forward). Database corruption could otherwise ' 'result. Are you sure you want to proceed? If so, type "%s" ' 'now.\n') % positive_confirmation elif options.command == WIPE: positive_confirmation = 'please delete my data' warn = ( 'This operation will wipe (i.e. DELETE!) the entire contents of ' 'the database pointed at by %s. Are you sure you want to proceed? ' 'If so, type "%s" now.\n') % (os.path.join( options.cred_dir, 'host.txt'), positive_confirmation) else: cros_build_lib.Die('No command or unsupported command. Exiting.') print(warn) conf_string = cros_build_lib.GetInput('(%s)?: ' % positive_confirmation) if conf_string != positive_confirmation: cros_build_lib.Die('You changed your mind. Aborting.') if options.command == MIGRATE: print('OK, applying migrations...') db = cidb.CIDBConnection(options.cred_dir) db.ApplySchemaMigrations(maxVersion=options.migrate_version) elif options.command == WIPE: print('OK, wiping database...') db = cidb.CIDBConnection(options.cred_dir) db.DropDatabase() print('Done.')
def testBooleanPrompt(self): self.mox.StubOutWithMock(cros_build_lib, 'GetInput') for value in ('', '', 'yes', 'ye', 'y', 'no', 'n'): cros_build_lib.GetInput(mox.IgnoreArg()).AndReturn(value) self.mox.ReplayAll() self.assertTrue(cros_build_lib.BooleanPrompt()) self.assertFalse(cros_build_lib.BooleanPrompt(default=False)) self.assertTrue(cros_build_lib.BooleanPrompt()) self.assertTrue(cros_build_lib.BooleanPrompt()) self.assertTrue(cros_build_lib.BooleanPrompt()) self.assertFalse(cros_build_lib.BooleanPrompt()) self.assertFalse(cros_build_lib.BooleanPrompt()) self.mox.VerifyAll()