Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break some various code out of the plugin
- Loading branch information
Showing
6 changed files
with
122 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import os.path | ||
import re | ||
|
||
from quickunit.utils import clean_bytecode_extension | ||
|
||
|
||
class FileChecker(dict): | ||
def __init__(self, rules, root=None): | ||
self.rules = rules | ||
self.changed_files = set() | ||
self.root = root | ||
self.compiled = False | ||
dict.__init__(self) | ||
|
||
def __missing__(self, filepath): | ||
self[filepath] = self.check(filepath) | ||
return self[filepath] | ||
|
||
def compile(self): | ||
self.compiled = True | ||
|
||
if self.root is None: | ||
try: | ||
filepath = self.changed_files.pop() | ||
except IndexError: | ||
self.root = '' | ||
else: | ||
self.changed_files.add(filepath) | ||
self.root = os.path.abspath(filepath)[:-len(filepath)] | ||
|
||
rules = [] | ||
for filepath in self.changed_files: | ||
try: | ||
path, filename = filepath.rsplit('/', 1) | ||
except ValueError: | ||
path, filename = '', filepath | ||
|
||
basename = filename.rsplit('.', 1)[0] | ||
|
||
params = { | ||
'path': path, | ||
'filename': filename, | ||
'basename': basename, | ||
} | ||
for rule in self.rules: | ||
rules.append(re.compile(rule.format(**params))) | ||
self.rules = rules | ||
|
||
def add(self, filepath): | ||
if self.compiled: | ||
raise Exception('The rules for this FileChecker are already compiled and files can no longer be added') | ||
self.changed_files.add(filepath) | ||
|
||
def check(self, filepath): | ||
filepath = clean_bytecode_extension(filepath) | ||
|
||
# check if this test was modified (e.g. added/changed) | ||
if self.root and filepath.startswith(self.root): | ||
filepath = filepath[len(self.root):] | ||
|
||
# if the filepath is actually the changed file | ||
if filepath in self.changed_files: | ||
return None | ||
|
||
for rule in self.rules: | ||
if rule.search(filepath): | ||
return None | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from collections import namedtuple | ||
|
||
ChangedFile = namedtuple('ChangedFile', ['filename', 'is_new']) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from subprocess import Popen, PIPE, STDOUT | ||
|
||
from quickunit.diff import DiffParser | ||
from quickunit.vcs.base import ChangedFile | ||
|
||
|
||
def parse_commit(parent=None): | ||
if parent is None: | ||
parent = 'master' | ||
|
||
proc = Popen(['git', 'merge-base', 'HEAD', parent], stdout=PIPE, stderr=STDOUT) | ||
|
||
parent_revision = proc.stdout.read().strip() | ||
|
||
# pull in our diff | ||
# git diff `git merge-base HEAD master` | ||
proc = Popen(['git', 'diff', parent_revision], stdout=PIPE, stderr=STDOUT) | ||
diff = proc.stdout.read().strip() | ||
|
||
parser = DiffParser(diff) | ||
|
||
files = [] | ||
for file in parser.parse(): | ||
if file['is_header']: | ||
continue | ||
|
||
# file was removed | ||
if file['new_filename'] == '/dev/null': | ||
continue | ||
|
||
filename = file['new_filename'][2:] | ||
|
||
is_new = (file['old_filename'] == '/dev/null') | ||
|
||
files.append(ChangedFile(filename, is_new)) | ||
|
||
return files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters