Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refactor): migrate all tests and test helper code to TS #649

Merged
merged 1 commit into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const shell = require('shelljs');
const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as shell from 'shelljs';

import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const shell = require('shelljs');
const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as shell from 'shelljs';

import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const shell = require('shelljs');
const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as shell from 'shelljs';

import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/tsdx-lint.test.js → test/e2e/tsdx-lint.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const shell = require('shelljs');
import * as shell from 'shelljs';

const util = require('../utils/fixture');
import * as util from '../utils/fixture';

shell.config.silent = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const shell = require('shelljs');
import * as shell from 'shelljs';

const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const shell = require('shelljs');
import * as shell from 'shelljs';

const util = require('../utils/fixture');
const { execWithCache, grep } = require('../utils/shell');
import * as util from '../utils/fixture';
import { execWithCache, grep } from '../utils/shell';

shell.config.silent = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const shell = require('shelljs');
const fs = require('fs-extra');
import * as fs from 'fs-extra';
import * as shell from 'shelljs';

const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
28 changes: 0 additions & 28 deletions test/utils/fixture.js

This file was deleted.

29 changes: 29 additions & 0 deletions test/utils/fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as path from 'path';
import * as shell from 'shelljs';

export const rootDir = process.cwd();

shell.config.silent = true;

export function setupStageWithFixture(
testDir: string,
stageName: string,
fixtureName: string
): void {
const stagePath = path.join(rootDir, stageName);
shell.mkdir(stagePath);
shell.exec(
`cp -a ${rootDir}/test/${testDir}/fixtures/${fixtureName}/. ${stagePath}/`
);
shell.ln(
'-s',
path.join(rootDir, 'node_modules'),
path.join(stagePath, 'node_modules')
);
shell.cd(stagePath);
}

export function teardownStage(stageName: string): void {
shell.cd(rootDir);
shell.rm('-rf', path.join(rootDir, stageName));
}
23 changes: 9 additions & 14 deletions test/utils/shell.js → test/utils/shell.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// this file contains helper utils for working with shell.js functions
const shell = require('shelljs');
import * as shell from 'shelljs';

shell.config.silent = true;

// simple shell.exec "cache" that doesn't re-run the same command twice in a row
let prevCommand = '';
let prevCommandOutput = {};
function execWithCache(command, { noCache = false } = {}) {
let prevCommandOutput = {} as shell.ShellReturnValue;
export function execWithCache(
command: string,
{ noCache = false } = {}
): shell.ShellReturnValue {
// return the old output
if (!noCache && prevCommand === command) return prevCommandOutput;

Expand All @@ -15,7 +18,7 @@ function execWithCache(command, { noCache = false } = {}) {
// reset if command is not to be cached
if (noCache) {
prevCommand = '';
prevCommandOutput = {};
prevCommandOutput = {} as shell.ShellReturnValue;
} else {
prevCommand = command;
prevCommandOutput = output;
Expand All @@ -24,19 +27,11 @@ function execWithCache(command, { noCache = false } = {}) {
return output;
}

// shelljs.grep wrapper
// @param {RegExp} pattern
// @param {string} fileName
// @returns {boolean} true if pattern has matches in file
function grep(pattern, fileName) {
// shell.js grep wrapper returns true if pattern has matches in file
export function grep(pattern: RegExp, fileName: string[]): boolean {
const output = shell.grep(pattern, fileName);
// output.code is always 0 regardless of matched/unmatched patterns
// so need to test output.stdout
// https://github.com/jaredpalmer/tsdx/pull/525#discussion_r395571779
return Boolean(output.stdout.match(pattern));
}

module.exports = {
execWithCache,
grep,
};