예제 #1
0
 def assert_directory_tree_correct(self, expected_tree, cwd=None):
     actual_tree = self.sourcetree.run_command('tree -I *.pyc --noreport', cwd)
     # special case for first listing:
     original_tree = expected_tree
     if expected_tree.startswith('superlists/'):
         expected_tree = Output(
             expected_tree.replace('superlists/', '.', 1)
         )
     self.assert_console_output_correct(actual_tree, expected_tree)
     original_tree.was_checked = True
 def test_ignores_phantomjs_run_times(self):
     actual = "Took 24ms to run 2 tests. 2 passed, 0 failed."
     expected = Output("Took 15ms to run 2 tests. 2 passed, 0 failed.")
     self.assert_console_output_correct(actual, expected)
     self.assertTrue(expected.was_checked)
 def test_tabs(self):
     expected = Output('#       bla bla')
     actual = '#\tbla bla'
     self.assert_console_output_correct(actual, expected)
     self.assertTrue(expected.was_checked)
 def test_working_directory_substitution(self):
     expected = Output('bla bla ...python-tdd-book/foo stuff')
     actual = f'bla bla {self.tempdir}/foo stuff'
     self.assert_console_output_correct(actual, expected)
     self.assertTrue(expected.was_checked)
 def test_ls(self):
     expected = Output('superlists          functional_tests.py')
     actual = 'functional_tests.py\nsuperlists\n'
     self.assert_console_output_correct(actual, expected, ls=True)
     self.assertTrue(expected.was_checked)
 def test_simple_case(self):
     actual = 'foo'
     expected = Output('foo')
     self.assert_console_output_correct(actual, expected)
     self.assertTrue(expected.was_checked)
예제 #7
0
    def recognise_listing_and_process_it(self):
        listing = self.listings[self.pos]
        if listing.dofirst:
            print("DOFIRST", listing.dofirst)
            self.sourcetree.patch_from_commit(listing.dofirst, )
        if listing.skip:
            print("SKIP")
            listing.was_checked = True
            listing.was_written = True
            self.pos += 1
        elif listing.type == 'test':
            print("TEST RUN")
            self.run_test_and_check_result()
        elif listing.type == 'git diff':
            print("GIT DIFF")
            self.check_diff_or_status(self.pos)
        elif listing.type == 'git status':
            print("STATUS")
            self.check_diff_or_status(self.pos)
        elif listing.type == 'git commit':
            print("COMMIT")
            self.check_commit(self.pos)

        elif listing.type == 'interactive manage.py':
            print("INTERACTIVE MANAGE.PY")
            output_before = self.listings[self.pos + 1]
            assert isinstance(output_before, Output)

            LIKELY_INPUTS = ('yes', 'no', '1', '2', "''")
            user_input = self.listings[self.pos + 2]
            if isinstance(user_input, Command) and user_input in LIKELY_INPUTS:
                if user_input == 'yes':
                    print('yes case')
                    # in this case there is moar output after the yes
                    output_after = self.listings[self.pos + 3]
                    assert isinstance(output_after, Output)
                    expected_output = Output(
                        wrap_long_lines(output_before + ' ' +
                                        output_after.lstrip()))
                    next_output = None
                elif user_input == '1':
                    print('migrations 1 case')
                    # in this case there is another hop
                    output_after = self.listings[self.pos + 3]
                    assert isinstance(output_after, Output)
                    first_input = user_input
                    next_input = self.listings[self.pos + 4]
                    assert isinstance(next_input, Command)
                    next_output = self.listings[self.pos + 5]
                    expected_output = Output(
                        wrap_long_lines(output_before + '\n' + output_after +
                                        '\n' + next_output))
                    user_input = Command(first_input + '\n' + next_input)
                else:
                    expected_output = output_before
                    output_after = None
                    next_output = None
                if user_input == '2':
                    ignore_errors = True
                else:
                    ignore_errors = False

            else:
                user_input = None
                expected_output = output_before
                output_after = None
                ignore_errors = True
                next_output = None

            output = self.run_command(listing,
                                      user_input=user_input,
                                      ignore_errors=ignore_errors)
            self.assert_console_output_correct(output, expected_output)

            listing.was_checked = True
            output_before.was_checked = True
            self.pos += 2
            if user_input is not None:
                user_input.was_run = True
                self.pos += 1
            if output_after is not None:
                output_after.was_checked = True
                self.pos += 1
            if next_output is not None:
                self.pos += 2
                next_output.was_checked = True
                first_input.was_run = True
                next_input.was_run = True

        elif listing.type == 'tree':
            print("TREE")
            self.assert_directory_tree_correct(listing)
            self.pos += 1

        elif listing.type == 'server command':
            server_output = self.run_server_command(listing)
            listing.was_run = True
            self.pos += 1
            next_listing = self.listings[self.pos]
            if next_listing.type == 'output' and not next_listing.skip:
                for line in next_listing.split('\n'):
                    assert line.strip() in server_output
                next_listing.was_checked = True
                self.pos += 1

        elif listing.type == 'other command':
            print("A COMMAND")
            output = self.run_command(listing)
            next_listing = self.listings[self.pos + 1]
            if next_listing.type == 'output' and not next_listing.skip:
                ls = listing.startswith('ls')
                self.assert_console_output_correct(output, next_listing, ls=ls)
                next_listing.was_checked = True
                listing.was_checked = True
                self.pos += 2
            elif 'tree' in listing and next_listing.type == 'tree':
                self.assert_console_output_correct(output, next_listing)
                next_listing.was_checked = True
                listing.was_checked = True
                self.pos += 2
            else:
                listing.was_checked = True
                self.pos += 1

        elif listing.type == 'diff':
            print("DIFF")
            self.apply_patch(listing)

        elif listing.type == 'code listing currentcontents':
            actual_contents = self.sourcetree.get_contents(listing.filename)
            print("CHECK CURRENT CONTENTS")
            stripped_actual_lines = [
                l.strip() for l in actual_contents.split('\n')
            ]
            listing_contents = re.sub(r' +#$',
                                      '',
                                      listing.contents,
                                      flags=re.MULTILINE)
            for line in listing_contents.split('\n'):
                if line and not '[...]' in line:
                    self.assertIn(line.strip(), stripped_actual_lines)
            listing.was_written = True
            self.pos += 1

        elif listing.type == 'code listing':
            print("CODE")
            self.write_to_file(listing)
            self.pos += 1

        elif listing.type == 'code listing with git ref':
            print("CODE FROM GIT REF")
            self.sourcetree.apply_listing_from_commit(listing)
            self.pos += 1

        elif listing.type == 'server code listing':
            print("SERVER CODE")
            self.write_file_on_server(listing.filename, listing.contents)
            self.pos += 1

        elif listing.type == 'qunit output':
            self.check_qunit_output(listing)
            self.pos += 1

        elif listing.type == 'output':
            self._strip_out_any_pycs()
            test_run = self.run_unit_tests()
            if 'OK' in test_run and not 'OK' in listing:
                print('unit tests pass, must be an FT:\n', test_run)
                test_run = self.run_fts()
            try:
                self.assert_console_output_correct(test_run, listing)
            except AssertionError as e:
                if 'OK' in test_run and 'OK' in listing:
                    print('got error when checking unit tests', e)
                    test_run = self.run_fts()
                    self.assert_console_output_correct(test_run, listing)
                else:
                    raise

            self.pos += 1

        else:
            self.fail('not implemented for ' + str(listing))