Skip to content

Commit 189ff03

Browse files
authored
feat(cicero-core): fix windows path normalization and increase test c… (#863)
* feat(cicero-core): fix windows path normalization and increase test coverage to 91% Signed-off-by: Rahul-R79 <rahul.devworks@gmail.com> * chore(cicero-core): address PR feedback, fix linting Signed-off-by: Rahul-R79 <rahul.devworks@gmail.com> --------- Signed-off-by: Rahul-R79 <rahul.devworks@gmail.com>
1 parent 6105a71 commit 189ff03

File tree

6 files changed

+228
-5
lines changed

6 files changed

+228
-5
lines changed

packages/cicero-core/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@
115115
],
116116
"all": true,
117117
"check-coverage": true,
118-
"statements": 82,
119-
"branches": 78,
120-
"functions": 76,
121-
"lines": 83
118+
"statements": 91,
119+
"branches": 81,
120+
"functions": 89,
121+
"lines": 91
122122
},
123123
"gitHead": "514fd02f46752e018aedb77e1716a212ef4a59e5"
124124
}

packages/cicero-core/src/templateloader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@ class TemplateLoader {
403403
}
404404
}
405405
else {
406-
if (regex.test(res)) {
406+
const slashPath = slash(res);
407+
if (regex.test(slashPath)) {
407408
return {
408409
name: res,
409410
contents: await TemplateLoader.loadFileContents(path, res, false, true)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
'use strict';
16+
17+
const Argument = require('../src/argument');
18+
const ArgumentType = require('../src/argumenttype');
19+
const CiceroFunction = require('../src/function');
20+
const Contract = require('../src/contract');
21+
22+
const chai = require('chai');
23+
chai.should();
24+
chai.use(require('chai-as-promised'));
25+
26+
describe('Core Classes', () => {
27+
describe('Argument', () => {
28+
it('should create an Argument', () => {
29+
const arg = new Argument('name', 'type');
30+
arg.getName().should.equal('name');
31+
arg.getType().should.equal('type');
32+
});
33+
});
34+
35+
describe('ArgumentType', () => {
36+
it('should create an ArgumentType', () => {
37+
const type = new ArgumentType('type');
38+
type.getName().should.equal('type');
39+
});
40+
});
41+
42+
describe('Function', () => {
43+
it('should create a Function', () => {
44+
const func = new CiceroFunction('name', []);
45+
func.getName().should.equal('name');
46+
func.getArguments().should.deep.equal([]);
47+
});
48+
});
49+
50+
describe('Contract', () => {
51+
it('should have a constructor', () => {
52+
Contract.should.not.be.null;
53+
});
54+
});
55+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
'use strict';
16+
17+
const DefaultArchiveLoader = require('../../src/loaders/defaultarchiveloader');
18+
19+
const chai = require('chai');
20+
chai.should();
21+
chai.use(require('chai-as-promised'));
22+
23+
describe('DefaultArchiveLoader', () => {
24+
let loader;
25+
26+
beforeEach(() => {
27+
loader = new DefaultArchiveLoader();
28+
});
29+
30+
describe('#constructor', () => {
31+
it('should create a DefaultArchiveLoader', () => {
32+
loader.should.not.be.null;
33+
});
34+
});
35+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
'use strict';
16+
17+
const GitHubArchiveLoader = require('../../src/loaders/githubarchiveloader');
18+
const mock = require('mock-require');
19+
20+
const chai = require('chai');
21+
chai.should();
22+
chai.use(require('chai-as-promised'));
23+
24+
describe('GitHubArchiveLoader', () => {
25+
let loader;
26+
27+
beforeEach(() => {
28+
loader = new GitHubArchiveLoader();
29+
});
30+
31+
describe('#accepts', () => {
32+
it('should accept a github URL', () => {
33+
loader.accepts('github://accordproject/githubarchiveloader').should
34+
.be.true;
35+
});
36+
37+
it('should not accept a non-github URL', () => {
38+
loader.accepts('http://accordproject/githubarchiveloader').should.be
39+
.false;
40+
});
41+
});
42+
43+
describe('#load', () => {
44+
it('should load an archive from github', async () => {
45+
const axiosMock = (request) => {
46+
return Promise.resolve({ data: Buffer.from('test') });
47+
};
48+
mock('axios', axiosMock);
49+
try {
50+
mock.reRequire('../../src/loaders/httparchiveloader');
51+
const MockedLoader = mock.reRequire(
52+
'../../src/loaders/githubarchiveloader',
53+
);
54+
const mockedLoader = new MockedLoader();
55+
const buffer = await mockedLoader.load(
56+
'github://accordproject/githubarchiveloader',
57+
);
58+
buffer.toString().should.equal('test');
59+
} finally {
60+
mock.stop('axios');
61+
}
62+
});
63+
});
64+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
'use strict';
16+
17+
const JSZip = require('jszip');
18+
const TemplateSaver = require('../src/templatesaver');
19+
const Template = require('../src/template');
20+
21+
const chai = require('chai');
22+
const expect = chai.expect;
23+
chai.should();
24+
chai.use(require('chai-as-promised'));
25+
26+
describe('TemplateSaver', () => {
27+
describe('#toArchive', () => {
28+
it('should save a template with a signature', async () => {
29+
const template = await Template.fromDirectory(
30+
'./test/data/latedeliveryandpenalty',
31+
);
32+
template.authorSignature = {
33+
templateHash: 'hash',
34+
timestamp: Date.now(),
35+
signatoryCert: 'cert',
36+
signature: 'sig',
37+
};
38+
const buffer = await TemplateSaver.toArchive(template);
39+
buffer.should.not.be.null;
40+
41+
// Verify the signature.json exists in the archive
42+
const zip = await JSZip.loadAsync(buffer);
43+
const sigFile = zip.file('signature.json');
44+
expect(sigFile).to.not.be.null;
45+
46+
const sigString = await sigFile.async('string');
47+
const sig = JSON.parse(sigString);
48+
sig.templateSignature.signature.should.equal('sig');
49+
});
50+
51+
it('should save a template with multiple locales', async () => {
52+
const template = await Template.fromDirectory(
53+
'./test/data/latedeliveryandpenalty',
54+
);
55+
template.getMetadata().getSamples().fr = 'Bonjour';
56+
const buffer = await TemplateSaver.toArchive(template);
57+
buffer.should.not.be.null;
58+
59+
// Verify the French sample text exists in the archive
60+
const zip = await JSZip.loadAsync(buffer);
61+
const sampleFile = zip.file('text/sample_fr.md');
62+
expect(sampleFile).to.not.be.null;
63+
64+
const content = await sampleFile.async('string');
65+
content.should.equal('Bonjour');
66+
});
67+
});
68+
});

0 commit comments

Comments
 (0)