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

Add test for generate.js #164

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions bin/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ if (args.h || args.help || args._.length > 1) {
const baseURL = args.b || 'https://query.linkeddatafragments.org/';
const webpackConfig = require(args.w ? path.resolve(process.cwd(), args.w) : '../webpack.config.js');

// Remove the location of queries.json which shouldn't be present in the build configuration.
webpackConfig[0].entry=webpackConfig[0].entry.filter(x => !x.endsWith('queries.json'));

// Override the baseURL in the webpack config
webpackConfig.baseURL.replace = baseURL;

Expand All @@ -66,6 +69,7 @@ if (args.h || args.help || args._.length > 1) {
entry.output.path = path.resolve(process.cwd(), destinationPath);
}
}

webpack(webpackConfig, (err, stats) => {
if (err) {
console.error(err.stack || err);
Expand All @@ -82,4 +86,12 @@ if (args.h || args.help || args._.length > 1) {

fs.unlinkSync('.tmp-comunica-engine.js');
});

if (fs.existsSync(path.join(process.cwd(), 'queries.json'))) {
// Create the build output folder if it doesn't exist
if (! fs.existsSync(path.resolve(process.cwd(), destinationPath))) {
fs.mkdirSync(path.resolve(process.cwd(), destinationPath));
}
fs.renameSync(path.join(process.cwd(), 'queries.json'), path.resolve(process.cwd(), `${destinationPath}/queries.json`));
};
})();
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"dev-prod": "comunica-compile-config config/config-default.json > .tmp-comunica-engine.js && ./bin/queries-to-json.js && webpack serve --config webpack.config.js --mode production",
"dev": "comunica-compile-config config/config-default.json > .tmp-comunica-engine.js && ./bin/queries-to-json.js && webpack serve --config webpack.config.js --mode development",
"build": "node ./bin/generate.js",
"version": "manual-git-changelog onversion"
"version": "manual-git-changelog onversion",
"test": "jest"
},
"dependencies": {
"@babel/core": "^7.16.0",
Expand All @@ -69,6 +70,7 @@
"@turf/centroid": "^6.5.0",
"babel-loader": "^8.2.3",
"file-loader": "^6.0.0",
"jest": "^29.7.0",
"json-loader": "^0.5.7",
"leaflet": "^1.7.1",
"minimist": "^1.2.5",
Expand Down
4 changes: 4 additions & 0 deletions test-queries/1.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
prefix ex: <http://example.org/>
SELECT ?a ?b WHERE {
?a ex:example ?b.
}
38 changes: 38 additions & 0 deletions tests/generate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { existsSync, readFileSync } from 'fs';
import { execSync } from 'child_process';

describe('Build test: generate.js', () => {
beforeAll(() => {
});

describe('generate.js', () => {
describe('command line arguments', () => {
beforeEach(() => {
execSync('rm -rf testBuildOutput');
});

afterEach(() => {
execSync('rm -rf testBuildOutput');
});

it('should correctly handle option -d', async() => {
execSync('node bin/generate.js -d testBuildOutput', { encoding: 'utf-8' }); // the default is 'buffer'
expect(existsSync('testBuildOutput')).toEqual(true);
});

it('should correctly handle option -q', async() => {
execSync('node bin/generate.js -d testBuildOutput -q tests/test-queries', { encoding: 'utf-8' }); // the default is 'buffer'
let buildQueries = JSON.parse(readFileSync("./build/queries.json")).queries;
expect(buildQueries).toEqual(
[
{
"name":"1",
"datasources":[],
"queryFormat":"sparql",
"query":"prefix ex: <http://example.org/>\nSELECT ?a ?b WHERE {\n ?a ex:example ?b.\n}"
}
]);
});
});
});
});
4 changes: 4 additions & 0 deletions tests/test-queries/1.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
prefix ex: <http://example.org/>
SELECT ?a ?b WHERE {
?a ex:example ?b.
}
Loading
Loading