Skip to content

Commit

Permalink
From travis to hooktest
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Apr 7, 2017
1 parent 4431478 commit fc30eb2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
5 changes: 5 additions & 0 deletions HookTest/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def parse_args(args):
parser.add_argument("--allowfailure",
help="Returns a passing test result as long as at least one text passes.",
action="store_true", default=False)
parser.add_argument(
"--from_travis_to_hook",
help="Send results to a Hook UI endpoint",
default=False
)

args = parser.parse_args(args)
if args.finder:
Expand Down
84 changes: 74 additions & 10 deletions HookTest/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def __init__(self, path,
repository=None, branch=None, uuid=None, workers=1, scheme="tei",
verbose=False, ping=None, secret="", triggering_size=None, console=False, travis=False,
finder=DefaultFinder, finderoptions=None, countwords=False, allowfailure=False,
from_travis_to_hook=False,
**kwargs
):
""" Create a Test object
Expand Down Expand Up @@ -168,7 +169,10 @@ def __init__(self, path,
self.uuid = uuid
self.workers = workers
self.ping = ping
self.secret = bytes(secret, "utf-8")
if os.environ.get("HOOK_SECRET"):
self.secret = os.environ.get("HOOK_SECRET").encode()
else:
self.secret = bytes(secret, "utf-8")
self.scheme = scheme
self.verbose = verbose
self.countwords = countwords
Expand Down Expand Up @@ -200,6 +204,8 @@ def __init__(self, path,
else:
self.finder = self.finder()

self.from_travis_to_hook = from_travis_to_hook

@property
def successes(self):
""" Get the number of successful tests
Expand Down Expand Up @@ -297,17 +303,19 @@ def count_files(self):
return len(self.text_files) + len(self.cts_files)

def flush(self, stack):
""" Flush the remaining logs to the endpoint """
""" Flush the remaining logs to the endpoint
"""
if len(stack) > 0:
for needle in stack:
needle.sent = True
self.send({"units": [needle.dict for needle in stack]})

def send(self, data):
"""
""" Send data to self.ping URL
:param data:
:return:
:param data: Data to send
:return: Result of request
"""
if isinstance(data, dict):
data = Test.dump(data)
Expand All @@ -316,7 +324,7 @@ def send(self, data):

data = bytes(data, "utf-8")
hashed = hmac.new(self.secret, data, hashlib.sha1).hexdigest()
requests.post(
return requests.post(
self.ping,
data=data,
headers={"HookTest-Secure-X": hashed, "HookTest-UUID": self.uuid}
Expand Down Expand Up @@ -456,16 +464,20 @@ def start(self):
})

def download(self):
""" Information to send or print during download
"""
if self.console:
print("\n".join([f for f in self.progress.json if f]), flush=True)

def middle(self):
"""
to print out the results for the metadata files that failed the tests
""" to print out the results for the metadata files that failed the tests
:return:
:rtype:
"""
self.m_files = self.m_passing = len(self.results.values())

if self.travis and self.verbose:
print('', flush=True)
if False not in [unit.status for unit in self.results.values()]:
Expand Down Expand Up @@ -596,8 +608,14 @@ def end(self):
for l, words in language_words.items():
results_table.add_row(["Words in {}".format(l.upper()), "{:,}".format(words)])
print(results_table, flush=True)
#print(black('#*# texts={texts} texts_passing={t_pass} metadata={meta} metadata_passing={m_pass} coverage_units={cov} total_nodes={nodes} words={words}'.format(
# texts=num_texts, t_pass=t_pass, meta=self.m_files, m_pass=self.m_passing, cov=cov, nodes="{:,}".format(total_units), words="{:,}".format(total_words))))

# Pushing to HOOK !
if isinstance(self.from_travis_to_hook, str):
args = [num_texts, t_pass, self.m_files, self.m_passing, cov, total_units]
if self.countwords is True:
args.append(language_words)
print(self.send_to_hook_from_travis(*args).text)

#Manifest of passing files
passing = self.create_manifest()
with open('{}/manifest.txt'.format(self.path), mode="w") as f:
Expand All @@ -615,6 +633,52 @@ def end(self):
report["units"] = [unit.dict for unit in self.stack]
self.send(report)

def send_to_hook_from_travis(
self, texts_total, texts_passing,
metadata_total, metadata_passing,
coverage, nodes_count,
words_dict=None
):
""" Send data to travis
:return: Request output
"""
data = dict(
# Event
event_type=os.environ.get("TRAVIS_EVENT_TYPE"),

travis_build_id=os.environ.get("TRAVIS_BUILD_NUMBER"),
travis_uri=os.environ.get("TRAVIS_BUILD_NUMBER"),
sha=os.environ.get("TRAVIS_COMMIT_MESSAGE"),

# Information about the test
texts_total=texts_total,
texts_passing=texts_passing,
metadata_total=metadata_total,
metadata_passing=metadata_passing,
coverage=coverage,
nodes_count=nodes_count,
units={
unit_name: log.status for unit_name, log in self.results.items()
},
)
if data["event_type"] == "pull_request":
data["source"] = os.environ.get("TRAVIS_PULL_REQUEST")
else:
data["source"] = os.environ.get("TRAVIS_BRANCH")

if words_dict is not None:
data["words_count"] = words_dict

data = Test.dump(data)
data = bytes(data, "utf-8")
hashed = hmac.new(self.secret, data, hashlib.sha1).hexdigest()
return requests.post(
self.from_travis_to_hook,
data=data,
headers={"HookTest-Secure-X": hashed}
)

def create_manifest(self):
""" Creates a manifest.txt file in the source directory that contains an ordered list of passing files
"""
Expand Down

0 comments on commit fc30eb2

Please sign in to comment.