Skip to content

Commit

Permalink
fix: add helper to read test fixtures
Browse files Browse the repository at this point in the history
Added file-helper to read test fixtures.
Moved fixtures into their own folder.
Added expected JSON to test inspect results fully.
Updated eslint versions to remove npm install warnings.
  • Loading branch information
gitphill committed Nov 21, 2019
1 parent 981b91a commit 79c3d85
Show file tree
Hide file tree
Showing 25 changed files with 373 additions and 208 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"license": "Apache-2.0",
"devDependencies": {
"@types/node": "11.13.8",
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
"eslint": "^6.5.1",
"eslint-config-prettier": "^6.6.0",
"@typescript-eslint/eslint-plugin": "^2.8.0",
"@typescript-eslint/parser": "^2.8.0",
"eslint": "^6.6.0",
"eslint-config-prettier": "^6.7.0",
"prettier": "^1.19.1",
"semantic-release": "^15",
"tap": "^12.0.1",
Expand Down
52 changes: 52 additions & 0 deletions tests/file-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as fs from 'fs';
import * as path from 'path';

/**
* Asynchronous fs.readFile.
*/
export async function readFile(path): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', function(err, data) {
if (err) {
reject(err);
}
resolve(data);
});
});
}

/**
* Read file and return contents parsed to JSON object.
*
* @param filePath path to file
*/
export async function readJSON(filePath: string) {
try {
const contents = await readFile(filePath);
return JSON.parse(contents);
} catch (err) {
throw new Error(
'Could not parse json file ' + filePath + '. ' + err.message,
);
}
}

/**
* Read test fixture file.
*
* @param fixturePath path relative to test fixtures directory.
*/
export async function readFixture(fixturePath: string) {
const filePath = path.join(__dirname, 'fixtures', fixturePath);
return await readFile(filePath);
}

/**
* Read test fixture file and parse to JSON.
*
* @param fixturePath path relative to test fixtures directory.
*/
export async function readFixtureJSON(fixturePath: string) {
const filePath = path.join(__dirname, 'fixtures', fixturePath);
return await readJSON(filePath);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"plugin": { "name": "bundled:maven", "runtime": "unknown" },
"package": {
"name": "fixtures.jars:spring-core-5.1.8.RELEASE.jar",
"version": "1.0.0-SNAPSHOT",
"dependencies": {
"org.springframework:spring-core": {
"version": "5.1.8.RELEASE",
"name": "org.springframework:spring-core",
"dependencies": {
"org.springframework:spring-jcl": {
"version": "5.1.8.RELEASE",
"name": "org.springframework:spring-jcl",
"dependencies": {},
"labels": { "scope": "compile" }
}
},
"labels": { "scope": "compile" }
}
},
"packageFormatVersion": "mvn:0.0.1"
}
}
23 changes: 23 additions & 0 deletions tests/fixtures/jars/jars:spring-core-5.1.8.RELEASE.jar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"plugin": { "name": "bundled:maven", "runtime": "unknown" },
"package": {
"name": "jars:spring-core-5.1.8.RELEASE.jar",
"version": "1.0.0-SNAPSHOT",
"dependencies": {
"org.springframework:spring-core": {
"version": "5.1.8.RELEASE",
"name": "org.springframework:spring-core",
"dependencies": {
"org.springframework:spring-jcl": {
"version": "5.1.8.RELEASE",
"name": "org.springframework:spring-jcl",
"dependencies": {},
"labels": { "scope": "compile" }
}
},
"labels": { "scope": "compile" }
}
},
"packageFormatVersion": "mvn:0.0.1"
}
}
15 changes: 0 additions & 15 deletions tests/fixtures/my-app/pom.xml

This file was deleted.

File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions tests/fixtures/path with spaces/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"plugin": { "name": "bundled:maven", "runtime": "unknown" },
"package": {
"name": "io.snyk.example:test-project",
"version": "1.0-SNAPSHOT",
"dependencies": {
"axis:axis": {
"version": "1.4",
"name": "axis:axis",
"dependencies": {
"org.apache.axis:axis-jaxrpc": {
"version": "1.4",
"name": "org.apache.axis:axis-jaxrpc",
"dependencies": {},
"labels": { "scope": "compile" }
},
"org.apache.axis:axis-saaj": {
"version": "1.4",
"name": "org.apache.axis:axis-saaj",
"dependencies": {},
"labels": { "scope": "compile" }
},
"axis:axis-wsdl4j": {
"version": "1.5.1",
"name": "axis:axis-wsdl4j",
"dependencies": {},
"labels": { "scope": "runtime" }
},
"commons-logging:commons-logging": {
"version": "1.0.4",
"name": "commons-logging:commons-logging",
"dependencies": {},
"labels": { "scope": "runtime" }
},
"commons-discovery:commons-discovery": {
"version": "0.2",
"name": "commons-discovery:commons-discovery",
"dependencies": {},
"labels": { "scope": "runtime" }
}
},
"labels": { "scope": "compile" }
}
},
"packageFormatVersion": "mvn:0.0.1"
}
}
60 changes: 60 additions & 0 deletions tests/fixtures/test-project/expected-with-dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"plugin": { "name": "bundled:maven", "runtime": "unknown" },
"package": {
"name": "io.snyk.example:test-project",
"version": "1.0-SNAPSHOT",
"dependencies": {
"axis:axis": {
"version": "1.4",
"name": "axis:axis",
"dependencies": {
"org.apache.axis:axis-jaxrpc": {
"version": "1.4",
"name": "org.apache.axis:axis-jaxrpc",
"dependencies": {},
"labels": { "scope": "compile" }
},
"org.apache.axis:axis-saaj": {
"version": "1.4",
"name": "org.apache.axis:axis-saaj",
"dependencies": {},
"labels": { "scope": "compile" }
},
"axis:axis-wsdl4j": {
"version": "1.5.1",
"name": "axis:axis-wsdl4j",
"dependencies": {},
"labels": { "scope": "runtime" }
},
"commons-logging:commons-logging": {
"version": "1.0.4",
"name": "commons-logging:commons-logging",
"dependencies": {},
"labels": { "scope": "runtime" }
},
"commons-discovery:commons-discovery": {
"version": "0.2",
"name": "commons-discovery:commons-discovery",
"dependencies": {},
"labels": { "scope": "runtime" }
}
},
"labels": { "scope": "compile" }
},
"junit:junit": {
"version": "4.10",
"name": "junit:junit",
"dependencies": {
"org.hamcrest:hamcrest-core": {
"version": "1.1",
"name": "org.hamcrest:hamcrest-core",
"dependencies": {},
"labels": { "scope": "test" }
}
},
"labels": { "scope": "test" }
}
},
"packageFormatVersion": "mvn:0.0.1"
}
}
47 changes: 47 additions & 0 deletions tests/fixtures/test-project/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"plugin": { "name": "bundled:maven", "runtime": "unknown" },
"package": {
"name": "io.snyk.example:test-project",
"version": "1.0-SNAPSHOT",
"dependencies": {
"axis:axis": {
"version": "1.4",
"name": "axis:axis",
"dependencies": {
"org.apache.axis:axis-jaxrpc": {
"version": "1.4",
"name": "org.apache.axis:axis-jaxrpc",
"dependencies": {},
"labels": { "scope": "compile" }
},
"org.apache.axis:axis-saaj": {
"version": "1.4",
"name": "org.apache.axis:axis-saaj",
"dependencies": {},
"labels": { "scope": "compile" }
},
"axis:axis-wsdl4j": {
"version": "1.5.1",
"name": "axis:axis-wsdl4j",
"dependencies": {},
"labels": { "scope": "runtime" }
},
"commons-logging:commons-logging": {
"version": "1.0.4",
"name": "commons-logging:commons-logging",
"dependencies": {},
"labels": { "scope": "runtime" }
},
"commons-discovery:commons-discovery": {
"version": "0.2",
"name": "commons-discovery:commons-discovery",
"dependencies": {},
"labels": { "scope": "runtime" }
}
},
"labels": { "scope": "compile" }
}
},
"packageFormatVersion": "mvn:0.0.1"
}
}
File renamed without changes.
26 changes: 11 additions & 15 deletions tests/functional/jar.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import * as test from 'tap-only';
import { isJar } from '../../lib/jar';

test('detects jar files', (t) => {
const jarPaths = [
test('isJar', async (t) => {
[
'mvn-app-1.0-SNAPSHOT.jar',
'mvn-app-1.0-SNAPSHOT.war',
'mvn-app-1.0-SNAPSHOT.zip',
];
for (const path of jarPaths) {
t.ok(isJar(path), path);
}
t.end();
});
'path/to/mvn-app-1.0-SNAPSHOT.jar',
'path/to/mvn-app-1.0-SNAPSHOT.war',
'path/to/mvn-app-1.0-SNAPSHOT.zip',
].forEach((i) => t.ok(isJar(i), 'should be true for ' + i));

test('ignores non jar files', (t) => {
const jarPaths = [
[
'mvn-app-1.0-SNAPSHOTjar',
'mvn-app-1.0-SNAPSHOT.txt',
'mvn-app-1.0-SNAPSHOT.jzip',
];
for (const path of jarPaths) {
t.ok(!isJar(path), path);
}
t.end();
'path/to/jar/mvn-app-1.0-SNAPSHOTjar',
'path/to/war/mvn-app-1.0-SNAPSHOTwar',
'path/to/zip/mvn-app-1.0-SNAPSHOTzip',
].forEach((i) => t.notOk(isJar(i), 'should be false for ' + i));
});
27 changes: 12 additions & 15 deletions tests/functional/mvn-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import * as test from 'tap-only';
import * as plugin from '../../lib';

test('check build args with array', (t) => {
test('buildArgs with array', async (t) => {
const result = plugin.buildArgs(null, null, ['-Paxis', '-Pjaxen']);
t.deepEquals(result, [
'dependency:tree',
'-DoutputType=dot',
'-Paxis',
'-Pjaxen',
]);
t.end();
t.same(
result,
['dependency:tree', '-DoutputType=dot', '-Paxis', '-Pjaxen'],
'should return expected array',
);
});

test('check build args with string', (t) => {
test('buildArgs with string', async (t) => {
const result = plugin.buildArgs(null, null, '-Paxis -Pjaxen');
t.deepEqual(result, [
'dependency:tree',
'-DoutputType=dot',
'-Paxis -Pjaxen',
]);
t.end();
t.same(
result,
['dependency:tree', '-DoutputType=dot', '-Paxis -Pjaxen'],
'should return expected array',
);
});
Loading

0 comments on commit 79c3d85

Please sign in to comment.