def test_range_filter_when_start_line_is_not_first(
        local_command: LocalCommandBuilder) -> None:
    local_command.path = PATH_TO_FILE
    local_command.format = 'json'
    local_command.start_line = 3

    output = run_in_subprocess(local_command.build())
    output_json = json.loads(output)

    expected_json_with_one_issue = {
        'quality': {
            'code': 'MODERATE',
            'text': 'Code quality (beta): MODERATE'
        },
        'issues': [
            {
                'code': 'E225',
                'text': 'missing whitespace around operator',
                'line': 'c=a + b',
                'line_number': 4,
                'column_number': 2,
                'category': 'CODE_STYLE',
                'influence_on_penalty': 0,
                'difficulty': "EASY",
            },
        ],
    }

    assert output_json == expected_json_with_one_issue
Esempio n. 2
0
def test_allow_duplicates(local_command: LocalCommandBuilder):
    file_with_duplicate_issue_path = DATA_PATH / 'duplicates' / 'code_with_duplicate_issues.py'

    local_command.allow_duplicates = True
    local_command.path = file_with_duplicate_issue_path

    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout_allow_duplicates = process.stdout.decode()

    local_command.allow_duplicates = False

    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout_filter_duplicates = process.stdout.decode()

    flake8_var_issue_re = re.compile(r'.*FLAKE8.*local variable \'var\'.*',
                                     re.DOTALL)
    pylint_var_issue_re = re.compile(r'.*PYLINT.*Unused variable \'var\'.*',
                                     re.DOTALL)

    assert len(stdout_filter_duplicates) < len(stdout_allow_duplicates)
    assert ((flake8_var_issue_re.match(stdout_allow_duplicates) is not None)
            and
            (pylint_var_issue_re.match(stdout_allow_duplicates) is not None))
    assert ((flake8_var_issue_re.match(stdout_filter_duplicates) is not None) ^
            (pylint_var_issue_re.match(stdout_filter_duplicates) is not None))
def test_range_filter_when_no_range_specified(
        local_command: LocalCommandBuilder) -> None:
    local_command.path = PATH_TO_FILE
    local_command.format = 'json'

    output = run_in_subprocess(local_command.build())
    output_json = json.loads(output)

    assert output_json == EXPECTED_JSON
def test_range_filter_when_end_line_is_last(
        local_command: LocalCommandBuilder) -> None:
    local_command.path = PATH_TO_FILE
    local_command.format = 'json'
    local_command.end_line = 4  # last line with an error

    output = run_in_subprocess(local_command.build())
    output_json = json.loads(output)

    assert output_json == EXPECTED_JSON
def test_range_filter_when_end_line_out_of_range(
        local_command: LocalCommandBuilder) -> None:
    local_command.path = PATH_TO_FILE
    local_command.format = 'json'
    local_command.end_line = 10

    output = run_in_subprocess(local_command.build())
    output_json = json.loads(output)

    assert output_json == output_json
def test_range_filter_when_start_line_is_first(
        local_command: LocalCommandBuilder) -> None:
    local_command.path = PATH_TO_FILE
    local_command.format = 'json'
    local_command.start_line = 1

    output = run_in_subprocess(local_command.build())
    output_json = json.loads(output)

    assert output_json == EXPECTED_JSON
def test_range_filter_when_both_start_and_end_lines_out_of_range(
        local_command: LocalCommandBuilder) -> None:
    local_command.path = PATH_TO_FILE
    local_command.format = 'json'
    local_command.start_line = 10
    local_command.end_line = 11

    output = run_in_subprocess(local_command.build())
    output_json = json.loads(output)

    assert output_json == NO_ISSUES_JSON
Esempio n. 8
0
def test_exit_code_zero(local_command: LocalCommandBuilder):
    file_path = DATA_PATH / 'exit_codes' / 'no_issues.py'

    local_command.path = file_path
    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )

    assert process.returncode == 0
Esempio n. 9
0
def test_exit_code_two(local_command: LocalCommandBuilder):
    file_path = Path('no_such_file.py')

    local_command.path = file_path
    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )

    assert process.returncode == 2
Esempio n. 10
0
def test_range_filter_when_both_start_and_end_lines_specified(
        local_command: LocalCommandBuilder) -> None:
    local_command.path = PATH_TO_FILE
    local_command.format = 'json'
    local_command.start_line = 1
    local_command.end_line = 5

    output = run_in_subprocess(local_command.build())
    output_json = json.loads(output)

    assert output_json == EXPECTED_JSON
Esempio n. 11
0
def test_range_filter_when_start_out_of_range(
        local_command: LocalCommandBuilder) -> None:
    local_command.path = PATH_TO_FILE
    local_command.format = 'json'
    local_command.start_line = 5

    output = run_in_subprocess(local_command.build())
    output_json = json.loads(output)

    expected_json_without_issues = NO_ISSUES_JSON

    assert output_json == expected_json_without_issues
Esempio n. 12
0
def test_range_filter_when_start_line_is_not_positive(
        local_command: LocalCommandBuilder) -> None:
    local_command.start_line = 0

    with pytest.raises(Exception):
        output = run_in_subprocess(local_command.build())
        json.loads(output)

    local_command.start_line = -1

    with pytest.raises(Exception):
        output = run_in_subprocess(local_command.build())
        json.loads(output)
def test_kotlin(local_command: LocalCommandBuilder):
    file_path = DATA_PATH / 'different_languages' / 'kotlin'

    local_command.path = file_path
    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output = process.stdout.decode()

    assert process.returncode == 1
    assert 'main.kt' in output
    assert 'person.kt' in output
def test_inspect_file_works(local_command: LocalCommandBuilder):
    file_path = DATA_PATH / 'file_or_project' / 'file.py'

    local_command.path = file_path

    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output = process.stdout.decode()

    assert process.returncode == 1
    assert file_path.name in output
def test_java(local_command: LocalCommandBuilder):
    file_path = DATA_PATH / 'different_languages' / 'java'

    local_command.path = file_path
    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output = process.stdout.decode()

    assert process.returncode == 1
    assert 'First.java' in output
    assert 'Second.java' in output
def test_python(local_command: LocalCommandBuilder):
    file_path = DATA_PATH / 'different_languages' / 'python'

    local_command.path = file_path
    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output = process.stdout.decode()

    assert process.returncode == 1
    assert 'a.py' in output
    assert 'b.py' in output
Esempio n. 17
0
def test_enable_all_logs(local_command: LocalCommandBuilder):
    file_path = DATA_PATH / 'verbosity' / 'some_code.py'

    local_command.verbosity = 3
    local_command.path = file_path

    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output = process.stdout.decode()
    output = output.lower()

    assert ' debug ' in output
Esempio n. 18
0
def test_disable_logs_json(local_command: LocalCommandBuilder):
    file_path = DATA_PATH / 'verbosity' / 'some_code.py'

    local_command.verbosity = 0
    local_command.format = 'json'
    local_command.path = file_path

    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )

    output = process.stdout.decode()
    json.loads(output)
Esempio n. 19
0
def test_disable_logs_text(local_command: LocalCommandBuilder):
    file_path = DATA_PATH / 'verbosity' / 'some_code.py'

    local_command.verbosity = 0
    local_command.path = file_path

    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output = process.stdout.decode()
    output = output.lower()

    assert ' debug ' not in output
    assert ' info ' not in output
    assert ' error ' not in output
Esempio n. 20
0
def test_range_filter_when_both_start_and_end_lines_specified_not_equal_borders(
    local_command: LocalCommandBuilder, ) -> None:
    local_command.path = PATH_TO_FILE
    local_command.format = 'json'
    local_command.start_line = 2
    local_command.end_line = 4

    output = run_in_subprocess(local_command.build())
    output_json = json.loads(output)

    expected_json = {
        'quality': {
            'code': 'BAD',
            'text': 'Code quality (beta): BAD',
        },
        'issues': [
            {
                'code': 'E225',
                'text': 'missing whitespace around operator',
                'line': 'b=20',
                'line_number': 2,
                'column_number': 2,
                'category': 'CODE_STYLE',
                'influence_on_penalty': 0,
                'difficulty': "EASY",
            },
            {
                'code': 'E225',
                'text': 'missing whitespace around operator',
                'line': 'c=a + b',
                'line_number': 4,
                'column_number': 2,
                'category': 'CODE_STYLE',
                'influence_on_penalty': 0,
                'difficulty': "EASY",
            },
        ],
    }

    assert output_json == expected_json
Esempio n. 21
0
def test_disable_works(local_command: LocalCommandBuilder):
    file_path = DATA_PATH / 'disable' / 'contains_flake8_issues.py'

    local_command.path = file_path
    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output = process.stdout.decode()

    assert 'FLAKE8' in output

    local_command.disable.append('flake8')
    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output = process.stdout.decode()

    assert 'FLAKE8' not in output
Esempio n. 22
0
def _get_output_json(local_command: LocalCommandBuilder, file_path: str, history: Optional[str] = None) -> Dict:
    file_path = DATA_PATH / 'difficulty_levels' / file_path

    local_command.verbosity = 0
    local_command.format = 'json'
    local_command.path = file_path
    local_command.new_format = False
    local_command.group_by_difficulty = True
    local_command.history = history

    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )

    return json.loads(process.stdout.decode())
Esempio n. 23
0
def _get_output_json(local_command: LocalCommandBuilder, new_format: bool,
                     group_by_difficulty: bool) -> str:
    project_path = DATA_PATH / 'file_or_project' / 'project'

    local_command.verbosity = 0
    local_command.format = 'json'
    local_command.path = project_path
    local_command.new_format = new_format
    local_command.group_by_difficulty = group_by_difficulty

    process = subprocess.run(
        local_command.build(),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout = process.stdout.decode()

    return json.loads(stdout)