Ejemplo n.º 1
0
def test_single_edit_to_file():
    '''Test that we can make a single change to a single file.'''
    project = ProjectNode(COPY_PATH)
    package = project.children['lib']
    nested_module = package.children['employee']

    code_monkey_class = nested_module.children['CodeMonkey']

    inject_source = \
        "\n    def like_tab_and_mountain_dew(self):\n        return True\n"

    changeset = ChangeSet()
    change = code_monkey_class.change.inject_at_line(
        6, inject_source)

    changeset.add(change)

    #check that diffs work as expected
    expected = EXPECTED_DIFF_SINGLE.format(code_monkey_class.fs_path)
    assert_equal(changeset.diff(), expected)

    changeset.commit()

    expected_file_path = path.join(RESOURCES_PATH, 'single_edit_expected')

    #check that the edit worked properly
    with open(code_monkey_class.fs_path) as modified_file:
        with open(expected_file_path) as expected_file:
            modified_source = modified_file.read()
            expected_source = expected_file.read()

            assert_equal(modified_source, expected_source)
Ejemplo n.º 2
0
def test_stacked_edits_to_file():
    '''Test that we can make multiple consecutive edits to a file in one
    ChangeSet.'''
    project = ProjectNode(COPY_PATH)

    employee_module = project.children['lib'].children['employee']
    employee_class = employee_module.children['Employee']
    code_monkey_class = employee_module.children['CodeMonkey']

    #we'll do the Employee with an inject_at_line directly, and CodeMonkey with
    #an inject_assignment, to get broader coverage

    employee_inject_source = "\n    FIRST_INJECTED_VALUE = 'foo'\n"

    change = employee_class.change.inject_at_line(
        1,
        employee_inject_source)

    second_change = code_monkey_class.change.inject_assignment(
        'SECOND_INJECTED_VALUE',
        ['bar'],
        line_index=1)

    changeset = ChangeSet()
    changeset.add([change, second_change])

    #check that diffs work as expected
    expected = EXPECTED_DIFF_STACKED.format(employee_module.fs_path)
    assert_equal(changeset.diff(), expected)

    #check that diff output works as expected:
    changeset.diff_to_file(path.join(COPY_PATH, 'diff'))
    with open(path.join(COPY_PATH, 'diff')) as diff_file:
        assert_equal(diff_file.read(), expected)

    changeset.commit()

    expected_file_path = path.join(RESOURCES_PATH, 'stacked_edit_expected')
    #check that the edit worked properly
    with open(employee_module.fs_path) as modified_file:
        with open(expected_file_path) as expected_file:
            modified_source = modified_file.read()
            expected_source = expected_file.read()

            assert_equal(modified_source, expected_source)