Skip to content

Commit

Permalink
Merge pull request #88 from launchableinc/go-test
Browse files Browse the repository at this point in the history
Add Go test plugin
  • Loading branch information
ninjinkun authored Jan 13, 2021
2 parents 66403bc + bd4f2a8 commit 4573661
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 2 deletions.
12 changes: 10 additions & 2 deletions launchable/commands/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self):
self.test_paths = []
# TODO: robustness improvement.
self._formatter = Optimize.default_formatter
self._separator = "\n"

@staticmethod
def default_formatter(x: TestPath):
Expand All @@ -74,6 +75,14 @@ def formatter(self) -> Callable[[TestPath], str]:
def formatter(self, v: Callable[[TestPath], str]):
self._formatter = v

@property
def separator(self) -> str:
return self._separator

@separator.setter
def separator(self, s: str):
self._separator = s

def test_path(self, path: TestPathLike):
"""register one test"""
self.test_paths.append(self.to_test_path(path))
Expand Down Expand Up @@ -160,7 +169,6 @@ def run(self):

# regardless of whether we managed to talk to the service
# we produce test names
for t in output:
click.echo(self.formatter(t))
click.echo(self.separator.join(self.formatter(t) for t in output))

context.obj = Optimize()
22 changes: 22 additions & 0 deletions launchable/test_runners/go_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys
import click
from . import launchable

@launchable.subset
def subset(client):
for case in sys.stdin:
# Avoid last line such as `ok github.com/launchableinc/rocket-car-gotest 0.268s`
if not ' ' in case:
client.test_path([{'type': 'testcase', 'name': case.rstrip('\n')}])

client.formatter = lambda x: "^{}$".format(x[0]['name'])
client.separator = '|'
client.run()


@click.argument('source_roots', required=True, nargs=-1)
@launchable.record.tests
def record_tests(client, source_roots):
for root in source_roots:
client.scan(root, "*.xml")
client.run()
72 changes: 72 additions & 0 deletions tests/commands/test_record_tests_go_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from unittest import TestCase, mock
from nose.tools import eq_, ok_
from click.testing import CliRunner
from test.support import captured_stdin

import os
import json
from pathlib import Path
import gzip

from launchable.__main__ import main


class GoTestTest(TestCase):
launchable_token = 'v1:launchableinc/mothership:auth-token-sample'
session = '/intake/organizations/launchableinc/workspaces/mothership/builds/123/test_sessions/16'
test_files_dir = Path(__file__).parent.joinpath(
'../data/go_test/').resolve()

def setUp(self):
self.maxDiff = None
os.environ['LAUNCHABLE_TOKEN'] = self.launchable_token

@mock.patch('requests.request')
def test_subset(self, mock_post):
runner = CliRunner()
pipe = "TestExample1\nTestExample2\nTestExample3\nTestExample4\nok github.com/launchableinc/rocket-car-gotest 0.268s"
result = runner.invoke(main, [
'subset', '--target', '10%', '--session', self.session, 'go_test'], input=pipe)

self.assertEqual(result.exit_code, 0)

for (args, kwargs) in mock_post.call_args_list:
if kwargs['data']:
data = kwargs['data']

result_file_path = self.test_files_dir.joinpath('subset_result.json')
with result_file_path.open() as json_file:
expected = json.load(json_file)
self.assertDictEqual(json.loads(data.decode()), expected)

@mock.patch('requests.request')
def test_record_tests(self, mock_post):
runner = CliRunner()
result = runner.invoke(main, ['record', 'tests', '--session',
self.session, 'go_test', str(self.test_files_dir) + "/"])
self.assertEqual(result.exit_code, 0)

for (args, kwargs) in mock_post.call_args_list:
if kwargs['data']:
data = kwargs['data']
zipped_payload = b''.join(data)
payload = json.loads(gzip.decompress(zipped_payload).decode())

result_file_path = self.test_files_dir.joinpath(
'record_test_result.json')
with result_file_path.open() as json_file:
expected = json.load(json_file)

# Normalize events order that depends on shell glob implementation
payload['events'] = sorted(
payload['events'], key=lambda c: c['testPath'][0]['name'])
expected['events'] = sorted(
expected['events'], key=lambda c: c['testPath'][0]['name'])

# Remove timestamp because it depends on the machine clock
for c in payload['events']:
del c['created_at']
for c in expected['events']:
del c['created_at']

self.assertDictEqual(payload, expected)
6 changes: 6 additions & 0 deletions tests/data/go_test/record_test_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{"events": [
{"type": "case", "testPath": [{"type": "class", "name": "rocket-car-gotest"}, {"type": "testcase", "name": "TestExample1", "_lineno": null}], "duration": 0.0, "status": 1, "stdout": "", "stderr": "", "created_at": "2021-01-12T13:12:33.410391+00:00", "data": null},
{"type": "case", "testPath": [{"type": "class", "name": "rocket-car-gotest"}, {"type": "testcase", "name": "TestExample2", "_lineno": null}], "duration": 3.0, "status": 1, "stdout": "", "stderr": "", "created_at": "2021-01-12T13:12:33.410811+00:00", "data": null},
{"type": "case", "testPath": [{"type": "class", "name": "rocket-car-gotest"}, {"type": "testcase", "name": "TestExample3", "_lineno": null}], "duration": 0.0, "status": 1, "stdout": "", "stderr": "", "created_at": "2021-01-12T13:12:33.410874+00:00", "data": null},
{"type": "case", "testPath": [{"type": "class", "name": "rocket-car-gotest"}, {"type": "testcase", "name": "TestExample4", "_lineno": null}], "duration": 2.0, "status": 1, "stdout": "", "stderr": "", "created_at": "2021-01-12T13:12:33.410917+00:00", "data": null}
]}
12 changes: 12 additions & 0 deletions tests/data/go_test/report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite tests="4" failures="0" time="5.262" name="github.com/launchableinc/rocket-car-gotest">
<properties>
<property name="go.version" value="go1.15.5"></property>
</properties>
<testcase classname="rocket-car-gotest" name="TestExample1" time="0.000"></testcase>
<testcase classname="rocket-car-gotest" name="TestExample2" time="3.000"></testcase>
<testcase classname="rocket-car-gotest" name="TestExample3" time="0.000"></testcase>
<testcase classname="rocket-car-gotest" name="TestExample4" time="2.000"></testcase>
</testsuite>
</testsuites>
6 changes: 6 additions & 0 deletions tests/data/go_test/subset_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{"session": {"id": "16"},
"target": 0.1,
"testPaths": [[{"name": "TestExample1", "type": "testcase"}],
[{"name": "TestExample2", "type": "testcase"}],
[{"name": "TestExample3", "type": "testcase"}],
[{"name": "TestExample4", "type": "testcase"}]]}

0 comments on commit 4573661

Please sign in to comment.