def test_early_terminate(self):
     bss = binary_search_state.MockBinarySearchState(
         get_initial_items='./gen_init_list.py',
         switch_to_good='./switch_to_good.py',
         switch_to_bad='./switch_to_bad.py',
         test_script='./is_good.py',
         prune=True,
         file_args=True,
         iterations=1)
     bss.DoSearch()
     self.assertFalse(bss.found_items)
 def test_verify_fail(self):
     bss = binary_search_state.MockBinarySearchState(
         get_initial_items='./gen_init_list.py',
         switch_to_good='./switch_to_bad.py',
         switch_to_bad='./switch_to_good.py',
         test_script='./is_good.py',
         prune=True,
         file_args=True,
         verify=True)
     with self.assertRaises(AssertionError):
         bss.DoVerify()
    def test_no_prune(self):
        bss = binary_search_state.MockBinarySearchState(
            get_initial_items='./gen_init_list.py',
            switch_to_good='./switch_to_good.py',
            switch_to_bad='./switch_to_bad.py',
            test_script='./is_good.py',
            test_setup_script='./test_setup.py',
            prune=False,
            file_args=True)
        bss.DoSearch()
        self.assertEquals(len(bss.found_items), 1)

        bad_objs = common.ReadObjectsFile()
        found_obj = int(bss.found_items.pop())
        self.assertEquals(bad_objs[found_obj], 1)
    def test_load_state(self):
        test_items = [1, 2, 3, 4, 5]

        bss = binary_search_state.MockBinarySearchState()
        bss.all_items = test_items
        bss.currently_good_items = set([1, 2, 3])
        bss.currently_bad_items = set([4, 5])
        bss.SaveState()

        bss = None

        bss2 = binary_search_state.MockBinarySearchState.LoadState()
        self.assertEquals(bss2.all_items, test_items)
        self.assertEquals(bss2.currently_good_items, set([]))
        self.assertEquals(bss2.currently_bad_items, set([]))
    def test_tmp_cleanup(self):
        bss = binary_search_state.MockBinarySearchState(
            get_initial_items='echo "0\n1\n2\n3"',
            switch_to_good='./switch_tmp.py',
            file_args=True)
        bss.SwitchToGood(['0', '1', '2', '3'])

        tmp_file = None
        with open('tmp_file', 'r') as f:
            tmp_file = f.read()
        os.remove('tmp_file')

        self.assertFalse(os.path.exists(tmp_file))
        ws = common.ReadWorkingSet()
        for i in range(3):
            self.assertEquals(ws[i], 42)
    def test_save_state(self):
        state_file = binary_search_state.STATE_FILE

        bss = binary_search_state.MockBinarySearchState()
        bss.SaveState()
        self.assertTrue(os.path.exists(state_file))
        first_state = os.readlink(state_file)

        bss.SaveState()
        second_state = os.readlink(state_file)
        self.assertTrue(os.path.exists(state_file))
        self.assertTrue(second_state != first_state)
        self.assertFalse(os.path.exists(first_state))

        bss.RemoveState()
        self.assertFalse(os.path.islink(state_file))
        self.assertFalse(os.path.exists(second_state))
    def test_bad_save_state(self):
        state_file = binary_search_state.STATE_FILE
        hidden_state_file = os.path.basename(
            binary_search_state.HIDDEN_STATE_FILE)

        with open(state_file, 'w') as f:
            f.write('test123')

        bss = binary_search_state.MockBinarySearchState()
        with self.assertRaises(binary_search_state.Error):
            bss.SaveState()

        with open(state_file, 'r') as f:
            self.assertEquals(f.read(), 'test123')

        os.remove(state_file)

        # Cleanup generated save state that has no symlink
        files = os.listdir(os.getcwd())
        save_states = [x for x in files if x.startswith(hidden_state_file)]
        _ = [os.remove(x) for x in save_states]