def test_multi_file(self): diff_content = [ "diff --git a/tests/path/to/testfile b/tests/path/to/testfile", "index 5f4d692..5b05678 100644", "--- a/tests/path/to/testfile", "+++ b/tests/path/to/testfile", "@@ -2,0 +2,1 @@", " 1", "+2", " 3", "@@ -13,7 +14,4 @@", " 4", "-5", "-6", "+7", " 8", "-9", "+10", "diff --git a/tests/path/to/secondfile b/tests/path/to/secondfile", "index 5f4d692..5b05678 100644", "--- a/tests/path/to/secondfile", "+++ b/tests/path/to/secondfile", "@@ -2,0 +2,1 @@", " 1", "+2", " 3", "@@ -13,7 +14,4 @@", " 4", "-5", "-6", "+7", " 8", "-9", "+10" ] self.assertEqual( diff.positions('tests', diff_content), { "path/to/testfile": { 2: 1, 3: 2, 4: 3, 14: 5, 15: 8, 16: 9, 17: 11 }, "path/to/secondfile": { 2: 1, 3: 2, 4: 3, 14: 5, 15: 8, 16: 9, 17: 11 }, })
def test_subtract_lines(self): diff_list = [ "diff --git a/testfile b/()", "@@ -1,6 +1,2 @@", " 1", "-2", "-3", " 4" ] self.assertEqual(diff.positions(diff_list, "testfile"), {1: 3, 2: 6})
def check(directory, diff_content, commit, verbosity): results = [] lint_results = Lint.find(directory=directory) diff_mappings = diff.positions(directory, diff_content) for filename, problems in lint_results.items(): file_seen = False if filename in diff_mappings: for problem in sorted(problems, key=lambda p: p.line): try: position = diff_mappings[filename][problem.line] if not file_seen: print(" * %s" % filename) file_seen = True print(' - %s' % problem) results.append((problem, position)) except KeyError: # Line doesn't exist in the diff; so we can ignore this problem if verbosity: if not file_seen: print(" * %s" % filename) file_seen = True print(' - Line %s not in diff' % problem.line) else: # File has been changed, but wasn't in the diff if verbosity: if not file_seen: print(" * %s" % filename) file_seen = True print(' - file not in diff') return results
def check(directory, diff_content, commit, verbosity): # Adapted from https://github.com/beeware/beefore/blob/master/src/beefore/checks/pycodestyle.py results = [] lint_results = Lint.find(directory=directory) diff_mappings = diff.positions(directory, diff_content) for filename, problems in lint_results.items(): file_seen = False if filename in diff_mappings: for problem in sorted(problems, key=lambda p: p.lineno): try: position = diff_mappings[filename][problem.lineno] if not file_seen: print(" * %s" % filename) file_seen = True print(" - %s" % problem) results.append((problem, position)) except KeyError: if verbosity: if not file_seen: print(" * %s" % filename) file_seen = True print(" - Line %s not in diff" % problem.lineno) else: if verbosity: if not file_seen: print(" * %s" % filename) file_seen = True print(" - file not in diff") return results
def check(directory, diff_content, commit, verbosity): results = [] lint_results = Lint.find(directory=directory) diff_mappings = diff.positions(directory, diff_content) for filename, problems in lint_results.items(): file_seen = False if filename in diff_mappings: for problem in sorted(problems, key=lambda p: p.line): try: position = diff_mappings[filename][problem.line] if not file_seen: print(" * %s" % filename) file_seen = True print(' - %s' % problem) results.append((problem, position)) except KeyError: # Line doesn't exist in the diff; so we can ignore this problem if verbosity: if not file_seen: print(" * %s" % filename) file_seen = True print(' - Line %s not in diff' % problem.line) else: # File has been changed, but wasn't in the diff if verbosity: if not file_seen: print(" * %s" % filename) file_seen = True print(' - file not in diff') return results
def test_add_subtract(self): diff_content = [ "diff --git a/tests/path/to/testfile b/tests/path/to/testfile", "index 5f4d692..5b05678 100644", "--- a/tests/path/to/testfile", "+++ b/tests/path/to/testfile", "@@ -2,0 +2,1 @@", " 1", "+2", " 3", "@@ -13,7 +14,4 @@", " 4", "-5", "-6", "+7", " 8", "-9", "+10" ] self.assertEqual( diff.positions('tests', diff_content), { "path/to/testfile": {2: 1, 3: 2, 4: 3, 14: 5, 15: 8, 16: 9, 17: 11} } )
def check(pull_request, commit, directory): problem_found = False diff_content = pull_request.diff().decode('utf-8').split('\n') for changed_file in commit.files: if os.path.splitext(changed_file['filename'])[-1] == '.js': print(" * %s" % changed_file['filename']) # Build a map of line numbers to diff positions diff_position = diff.positions(diff_content, changed_file['filename']) problems = Lint.find( directory=directory, filename=changed_file['filename'], ) for problem in problems: try: position = diff_position[problem.line] problem_found = True print(' - %s' % problem) problem.add_comment(pull_request, commit, position) except KeyError: # Line doesn't exist in the diff; so we can ignore this problem print(' - [IGNORED] %s' % problem) return not problem_found
def test_subtract_lines(self): diff_content = [ "diff --git a/tests/path/to/testfile b/tests/path/to/testfile", "@@ -1,6 +1,2 @@", " 1", "-2", "-3", " 4" ] self.assertEqual(diff.positions('tests', diff_content), {"path/to/testfile": { 1: 1, 2: 4 }})
def test_no_diff(self): diff_content = [ "1", "2", "3", "4" ] self.assertEqual( diff.positions('tests', diff_content), {} )
def test_add_subtract(self): diff_list = [ "diff --git a/testfile b/()", " 1", "@@ -2,0 +2,1 @@", "+2", " 3", "@@ -3,7 +4,4 @@", " 4", "-5", "-6", "+7", " 8", "-9", "+10" ] self.assertEqual(diff.positions(diff_list, "testfile"), { 2: 4, 3: 5, 4: 7, 5: 10, 6: 11, 7: 13 })
def test_add_lines(self): diff_list = [ "diff --git a/testfile b/()", "@@ -1,4 +1,6 @@", " 1", "+2", "+3", " 4", " 5", " 6" ] self.assertEqual(diff.positions(diff_list, "testfile"), { 1: 3, 2: 4, 3: 5, 4: 6, 5: 7, 6: 8 })
def test_subtract_lines(self): diff_content = [ "diff --git a/tests/path/to/testfile b/tests/path/to/testfile", "@@ -1,6 +1,2 @@", " 1", "-2", "-3", " 4" ] self.assertEqual( diff.positions('tests', diff_content), { "path/to/testfile": {1: 1, 2: 4} } )
def test_add_lines(self): diff_content = [ "diff --git a/tests/path/to/testfile b/tests/path/to/testfile", "@@ -1,4 +1,6 @@", " 1", "+2", "+3", " 4", " 5", " 6" ] self.assertEqual( diff.positions('tests', diff_content), {"path/to/testfile": { 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6 }})
def test_add_lines(self): diff_content = [ "diff --git a/tests/path/to/testfile b/tests/path/to/testfile", "@@ -1,4 +1,6 @@", " 1", "+2", "+3", " 4", " 5", " 6" ] self.assertEqual( diff.positions('tests', diff_content), { "path/to/testfile": {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6} } )
def check(pull_request, commit, directory, config): problem_found = False diff_content = pull_request.diff().decode('utf-8').split('\n') for changed_file in commit.files: if os.path.splitext(changed_file['filename'])[-1] == '.js': print (" * %s" % changed_file['filename']) # Build a map of line numbers to diff positions diff_position = diff.positions(diff_content, changed_file['filename']) # If a directory has been provided, use that as the source of # the files. Otherwise, download the file blob. if directory is None: response = requests.get(changed_file['raw_url']) content = response.content else: with open(os.path.join(directory, changed_file['filename'])) as fp: content = fp.read().encode('utf-8') problems = Lint.find( filename=changed_file['filename'], content=content, config=config ) print(diff_position) for problem in problems: try: print(problem.line) position = diff_position[problem.line] print(' - %s' % problem) problem.add_comment(pull_request, commit, position) except KeyError: # Line doesn't exist in the diff; so we can ignore this problem pass return problem_found
def check(pull_request, commit, directory, config): problem_found = False diff_content = pull_request.diff().decode('utf-8').split('\n') for changed_file in commit.files: if os.path.splitext(changed_file['filename'])[-1] == '.js': print(" * %s" % changed_file['filename']) # Build a map of line numbers to diff positions diff_position = diff.positions(diff_content, changed_file['filename']) # If a directory has been provided, use that as the source of # the files. Otherwise, download the file blob. if directory is None: response = requests.get(changed_file['raw_url']) content = response.content else: with open(os.path.join(directory, changed_file['filename'])) as fp: content = fp.read().encode('utf-8') problems = Lint.find(filename=changed_file['filename'], content=content, config=config) print(diff_position) for problem in problems: try: print(problem.line) position = diff_position[problem.line] print(' - %s' % problem) problem.add_comment(pull_request, commit, position) except KeyError: # Line doesn't exist in the diff; so we can ignore this problem pass return problem_found
def test_no_diff(self): diff_content = ["1", "2", "3", "4"] self.assertEqual(diff.positions('tests', diff_content), {})
def test_wrong_filename(self): diff_list = ["1", "2", "3", "4"] self.assertEqual(diff.positions(diff_list, "wrong"), None)
def test_no_change(self): diff_list = ["1", "2", "3", "4"] self.assertEqual(diff.positions(diff_list, "testfile"), None)