Skip to content

Commit

Permalink
test: add tests for glob patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumolari committed Oct 1, 2020
1 parent 22c529e commit 2327fa1
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 0 deletions.
140 changes: 140 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
"devDependencies": {
"@types/node": "^14.11.2",
"@types/node-fetch": "^2.5.7",
"@types/sinon": "^9.0.7",
"@types/tape": "^4.13.0",
"nock": "^13.0.4",
"prettier": "^2.1.2",
"sinon": "^9.1.0",
"tape": "^5.0.1",
"to-readable-stream": "^2.1.0",
"ts-node": "^9.0.0",
Expand Down
95 changes: 95 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import test from 'tape';
import nock from 'nock';
import toReadableStream from 'to-readable-stream';
import { default as hookStd } from 'hook-std';
import * as glob from '@actions/glob';
import sinon from 'sinon';
import { tmpdir } from 'os';
import { resolve as resolvePath } from 'path';
import {
stat as statCallback,
writeFileSync,
unlinkSync,
realpath as realpathCallback,
} from 'fs';
Expand All @@ -28,6 +32,8 @@ const realpath = promisify(realpathCallback);
const DEFAULT_WORKDIR = process.cwd();
let DEFAULT_ECHO = '/bin/echo';

const sandbox = sinon.createSandbox();

test('🛠 setup', (t) => {
nock.disableNetConnect();
pExec('which echo', (err, stdout, stderr) => {
Expand Down Expand Up @@ -158,6 +164,95 @@ after-build --exit-code 0
t.end();
});

test('🧪 run() should convert patterns to locations.', async (t) => {
t.plan(3);
const globSpy = sandbox
.stub()
.resolves([
resolvePath(__dirname, './file-a.lcov'),
resolvePath(__dirname, './file-b.lcov'),
]);
sandbox.stub(glob, 'create').resolves({
glob: globSpy,
getSearchPaths: sandbox.spy(),
globGenerator: sandbox.spy(),
});
const filePath = './test.sh';
nock('http://localhost.test')
.get('/dummy-cc-reporter')
.reply(200, () => {
return toReadableStream(`#!/bin/bash
echo "$*"
`); // Dummy shell script that just echoes back all arguments.
});

const filePattern = './*.lcov:lcov';
const fileA = 'file-a.lcov';
const fileB = 'file-b.lcov';

writeFileSync(fileA, 'file a content');
writeFileSync(fileB, 'file b content');

let capturedOutput = '';
const stdHook = hookStd((text: string) => {
capturedOutput += text;
});

try {
await run(
'http://localhost.test/dummy-cc-reporter',
filePath,
'',
'',
'false',
filePattern
);
stdHook.unhook();
} catch (err) {
stdHook.unhook();
t.fail(err);
} finally {
nock.cleanAll();
}

t.deepEquals(
((glob.create as unknown) as sinon.SinonSpy).firstCall.firstArg,
'./*.lcov',
'should create a globber with given pattern'
);
t.true(
globSpy.calledOnceWithExactly(),
'should get the paths of the files from the newly created globber instance'
);
t.equal(
capturedOutput,
// prettier-ignore
`::debug::ℹ️ Downloading CC Reporter from http://localhost.test/dummy-cc-reporter ...
::debug::✅ CC Reporter downloaded...
[command]${resolvePath(__dirname, '../test.sh')} before-build
before-build
::debug::✅ CC Reporter before-build checkin completed...
ℹ️ 'coverageCommand' not set, so skipping building coverage report!
::debug::Parsing 2 coverage location(s) — ${resolvePath(__dirname, 'file-a.lcov')}:lcov,${resolvePath(__dirname, 'file-b.lcov')}:lcov (object)
[command]${resolvePath(__dirname, '../test.sh')} format-coverage ${resolvePath(__dirname, 'file-a.lcov')} -t lcov -o codeclimate.0.json
format-coverage ${resolvePath(__dirname, 'file-a.lcov')} -t lcov -o codeclimate.0.json
[command]${resolvePath(__dirname, '../test.sh')} format-coverage ${resolvePath(__dirname, 'file-b.lcov')} -t lcov -o codeclimate.1.json
format-coverage ${resolvePath(__dirname, 'file-b.lcov')} -t lcov -o codeclimate.1.json
[command]${resolvePath(__dirname, '../test.sh')} sum-coverage codeclimate.0.json codeclimate.1.json -p 2 -o coverage.total.json
sum-coverage codeclimate.0.json codeclimate.1.json -p 2 -o coverage.total.json
[command]${resolvePath(__dirname, '../test.sh')} upload-coverage -i coverage.total.json
upload-coverage -i coverage.total.json
::debug::✅ CC Reporter upload coverage completed!
`,
'should execute all steps (except running the coverage command).'
);
unlinkSync(filePath);
unlinkSync(fileA);
unlinkSync(fileB);
nock.cleanAll();
t.end();
});

test('🧪 run() should correctly switch the working directory if given.', async (t) => {
t.plan(1);
const filePath = './test.sh';
Expand Down

0 comments on commit 2327fa1

Please sign in to comment.