Skip to content

Commit

Permalink
Merge v3.0.1 changes into branch v3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ldgit committed May 19, 2020
2 parents 262df99 + b65f1e9 commit d6e5c0e
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- "12"
- "13"
- "14"
script:
- npm run test:all
after_success: npm run coverage
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "argus-test-runner",
"version": "3.0.0",
"version": "3.0.1",
"description": "Watches for changes in your source and test files and executes automatic tests",
"main": "index.js",
"author": "Danko Lučić",
Expand Down
51 changes: 33 additions & 18 deletions src/test-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@ function findTestsFor(environments, filePath) {
.filter(testPath => fs.existsSync(testPath.path));
}

function getPossibleTestPath(filePath, environment) {
const normalizedFilePath = path.normalize(filePath).replace(/\\/g, '/');
const normalizedTestDir = path.normalize(environment.testDir).replace(/\\/g, '/');

if (normalizedFilePath.startsWith(normalizedTestDir)) {
return path.join(normalizedFilePath);
}

return [
path.join(environment.testDir, pathWithoutExtensionAndSourceDir(filePath, environment)),
`${environment.testNameSuffix}.${environment.extension}`,
].join('');
}

function getEnvironmentsForFile(filePath, environments) {
const foundEnvironments = [];
const fileExtension = filePath.split('.').pop().toLowerCase();
Expand All @@ -40,10 +26,39 @@ function getEnvironmentsForFile(filePath, environments) {
return foundEnvironments;
}

function pathWithoutExtensionAndSourceDir(filePath, environment) {
return removeSourceDirFromPath(filePath, environment).split('.').slice(0, -1).join('.');
function getPossibleTestPath(filePath, { testNameSuffix, testDir, sourceDir, extension }) {
const normalizedFilePath = path.normalize(filePath).replace(/\\/g, '/');
const normalizedTestDir = path.normalize(testDir).replace(/\\/g, '/');

if (
isInTestDir(normalizedFilePath, normalizedTestDir) &&
isTestFile(normalizedFilePath, testNameSuffix)
) {
return path.join(normalizedFilePath);
}

return [
path.join(testDir, pathWithoutExtensionAndSourceDir(filePath, sourceDir)),
`${testNameSuffix}.${extension}`,
].join('');
}

function isInTestDir(filePath, testDir) {
return filePath.startsWith(testDir);
}

function isTestFile(filePath, testSuffix) {
return getWithoutExtension(filePath).endsWith(testSuffix);
}

function pathWithoutExtensionAndSourceDir(filePath, sourceDir) {
return getWithoutExtension(removeSourceDirFromPath(filePath, sourceDir));
}

function getWithoutExtension(filePath) {
return filePath.split('.').slice(0, -1).join('.');
}

function removeSourceDirFromPath(filePath, environment) {
return filePath.replace(environment.sourceDir, '');
function removeSourceDirFromPath(filePath, sourceDir) {
return filePath.replace(sourceDir, '');
}
Empty file.
50 changes: 50 additions & 0 deletions test/integration/argus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,54 @@ describe('argus', function argusTestSuite() {
});
});
});

context('tests-and-source-together', () => {
let runCommandsSpy;

beforeEach(() => {
process.chdir(path.join('.', 'test', 'integration', 'fixtures', 'tests-and-source-together'));
runCommandsSpy = createRunCommandsSpy();
runArgus = configureRunArgus(runCommandsSpy, commandLineOptions, mockStdin);
});

it('should watch project with test and source next to each other', async () => {
watcher = runArgus();
fork(pathToTouchScript, [path.join('.', 'src', 'app.js')]);

const { commandCount, firstCommand } = await new Promise(resolve => {
watcher.on('change', () => {
waitForDebounce().then(() => {
resolve({
commandCount: runCommandsSpy.getLastRunCommands().length,
firstCommand: runCommandsSpy.getLastRunCommands()[0],
});
});
});
});

expect(commandCount).to.equal(1, 'Expected one command to run');
expect(firstCommand.command).to.equal('echo "I test thee"');
expect(firstCommand.args).to.eql(['src/app.test.js'].map(filepath => path.join(filepath)));
});

it('should watch project with test and source next to each other (test file changed)', async () => {
watcher = runArgus();
fork(pathToTouchScript, [path.join('.', 'src', 'app.test.js')]);

const { commandCount, firstCommand } = await new Promise(resolve => {
watcher.on('change', () => {
waitForDebounce().then(() => {
resolve({
commandCount: runCommandsSpy.getLastRunCommands().length,
firstCommand: runCommandsSpy.getLastRunCommands()[0],
});
});
});
});

expect(commandCount).to.equal(1, 'Expected one command to run');
expect(firstCommand.command).to.equal('echo "I test thee"');
expect(firstCommand.args).to.eql(['src/app.test.js'].map(filepath => path.join(filepath)));
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
environments: [
{
extension: 'js',
testNameSuffix: '.test',
testDir: 'src',
sourceDir: 'src',
testRunnerCommand: { command: 'echo "I test thee"' },
},
],
};
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions test/test-finder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ describe('test-finder', () => {
{ path: path.join('tests/unit/src/ExampleTwoTest.php'), environment: phpEnvironment },
]);
});

it('should work when source and test are in the same dir', () => {
const environment = createEnvironment('js', 'src', '.test', 'src');
findTestsFor = configureFindTestsFor([environment]);

assertTestFound(findTestsFor('src/ExampleFour.js'), 'src/ExampleFour.test.js', environment);
});
});

function assertTestFound(actualTests, expectedTestPath, expectedEnvironment = phpEnvironment) {
Expand Down

0 comments on commit d6e5c0e

Please sign in to comment.