review/tests/test_comment.py @ 08528309f7cd

Fix the path sanitization and add some tests.
author Steve Losh <steve@stevelosh.com>
date Wed, 07 Oct 2009 19:08:50 -0400
parents 34caeeba9ae2
children 85ab60753c33
from nose import *
from util import *
from .. import messages

import os
from mercurial import util as hgutil


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_no_comments():
    sandbox = get_sandbox_repo()
    
    output = review()
    assert messages.REVIEW_LOG_COMMENTS % (0, 0) in output


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_blank_comment():
    sandbox = get_sandbox_repo()
    
    try:
        review(comment=True)
    except hgutil.Abort, e:
        error = str(e)
        assert messages.COMMENT_REQUIRES_MESSAGE in error
    else:
        assert False, 'The correct error message was not printed.'


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_add_comments_to_parent_rev():
    sandbox = get_sandbox_repo()
    
    review(comment=True, message='Test comment one.')
    
    output = review()
    assert messages.REVIEW_LOG_COMMENTS % (1, 1) in output
    
    author_line = messages.REVIEW_LOG_COMMENT_AUTHOR % '|'
    a1, _, a2 = author_line.partition('|')
    assert a1 in output
    assert a2 in output
    
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    
    review(comment=True, message='Test comment two.')
    
    output = review()
    assert messages.REVIEW_LOG_COMMENTS % (2, 1) in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment two.' in output


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_add_comments_to_specific_rev():
    sandbox = get_sandbox_repo()
    
    review(comment=True, message='Test comment one.', rev='0')
    
    output = review(rev='0')
    assert messages.REVIEW_LOG_COMMENTS % (1, 1) in output
    
    author_line = messages.REVIEW_LOG_COMMENT_AUTHOR % '|'
    a1, _, a2 = author_line.partition('|')
    assert a1 in output
    assert a2 in output
    
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    
    output = review()
    assert messages.REVIEW_LOG_COMMENTS % (0, 0) in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output
    
    review(comment=True, message='Test comment two.', rev='0')
    
    output = review(rev='0')
    assert messages.REVIEW_LOG_COMMENTS % (2, 1) in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment two.' in output


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_add_comments_to_file():
    sandbox = get_sandbox_repo()
    
    author_line = messages.REVIEW_LOG_COMMENT_AUTHOR % '|'
    a1, _, a2 = author_line.partition('|')
    
    review(comment=True, message='Test comment one.', rev='1', files=['file_one'])
    
    output = review(rev='1', files=['file_one'])
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    
    output = review(rev='1', files=['file_two'])
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output
    
    output = review(rev='0', files=['file_one'])
    assert a1 not in output
    assert a2 not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_add_comments_to_bad_file():
    sandbox = get_sandbox_repo()
    
    try:
        review(comment=True, message='Test comment one.', files=['bad'])
    except hgutil.Abort, e:
        error = str(e)
        assert messages.COMMENT_FILE_DOES_NOT_EXIST % ('bad', '2') in error
    else:
        assert False, 'The correct error message was not printed.'
    


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_add_comments_to_file_line():
    sandbox = get_sandbox_repo()
    
    try:
        review(comment=True, rev='1', message='Test bad comment.', lines='1')
    except hgutil.Abort, e:
        error = str(e)
        assert messages.COMMENT_LINES_REQUIRE_FILE in error
    else:
        assert False, 'The correct error message was not printed.'
    
    author_line = messages.REVIEW_LOG_COMMENT_AUTHOR % '|'
    a1, _, a2 = author_line.partition('|')
    
    review(comment=True, rev='1', message='Test comment one.',
        files=['file_one'], lines='1')
    
    output = review(rev='1', files=['file_one'])
    
    # Make sure the comment is present at all.
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    
    # Make sure it's in the correct place
    output = output.splitlines()
    for n, line in enumerate(output):
        if line.startswith('#'):
            assert output[n-1].strip().startswith('1')
            break


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_add_comments_to_file_lines():
    sandbox = get_sandbox_repo()
    
    author_line = messages.REVIEW_LOG_COMMENT_AUTHOR % '|'
    a1, _, a2 = author_line.partition('|')
    
    review(comment=True, rev='1', message='Test comment one.',
        files=['file_one'], lines='1,2')
    
    output = review(rev='1', files=['file_one'])
    
    # Make sure the comment is present at all.
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    
    # Make sure it's in the correct place
    output = output.splitlines()
    for n, line in enumerate(output):
        if line.startswith('#'):
            assert output[n-1].strip().startswith('2')
            break


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_add_comments_to_file_in_subdir():
    sandbox = get_sandbox_repo()
    
    author_line = messages.REVIEW_LOG_COMMENT_AUTHOR % '|'
    a1, _, a2 = author_line.partition('|')
    
    filename = os.path.join('test_dir', 'test_file')
    
    review(comment=True, message='Test comment one.', rev='1', files=[filename])
    
    output = review(rev='1', files=[filename])
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    
    output = review(rev='1', files=['file_two'])
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output
    
    output = review(rev='0', files=[filename])
    assert a1 not in output
    assert a2 not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_add_comments_to_file_in_cwd():
    sandbox = get_sandbox_repo()
    
    author_line = messages.REVIEW_LOG_COMMENT_AUTHOR % '|'
    a1, _, a2 = author_line.partition('|')
    
    os.chdir('test_dir')
    
    review(comment=True, message='Test comment one.', rev='1', files=['test_file'])
    
    output = review(rev='1', files=['test_file'])
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    
    output = review(rev='1', files=['file_two'])
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output
    
    output = review(rev='0', files=['test_file'])
    assert a1 not in output
    assert a2 not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output


@with_setup(setup_reviewed_sandbox, teardown_sandbox)
def test_add_comments_to_file_in_reldir():
    sandbox = get_sandbox_repo()
    
    author_line = messages.REVIEW_LOG_COMMENT_AUTHOR % '|'
    a1, _, a2 = author_line.partition('|')
    
    filename = os.path.join('..', 'file_three')
    
    os.chdir('test_dir')
    
    review(comment=True, message='Test comment one.', rev='1', files=[filename])
    
    output = review(rev='1', files=[filename])
    assert a1 in output
    assert a2 in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' in output
    
    output = review(rev='1', files=['file_two'])
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output
    
    output = review(rev='0', files=[filename])
    assert a1 not in output
    assert a2 not in output
    assert messages.REVIEW_LOG_COMMENT_LINE % 'Test comment one.' not in output