Skip to content

Commit e913d21

Browse files
authored
Merge pull request #92 from electricimp/develop
@include functionality revised, improved and fully described in the readme __PATH__ variable fixed according to its definition in the readme existing tests updated additional tests developed to fully test @include functionality
2 parents 4e55bf1 + def898d commit e913d21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1187
-335
lines changed

Diff for: README.md

+427-240
Large diffs are not rendered by default.

Diff for: package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Builder",
3-
"version": "3.3.0",
3+
"version": "4.0.0",
44
"description": "Builder Language Implementation",
55
"main": "src/index.js",
66
"bin": {

Diff for: spec/Builder.spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
// Copyright (c) 2016-2017 Electric Imp
1+
// Copyright (c) 2016-2020 Electric Imp
22
// This file is licensed under the MIT License
33
// http://opensource.org/licenses/MIT
44

55
'use strict';
66

77
const Builder = require('../src');
88
const Machine = require('../src/Machine');
9+
const path = require('path');
10+
11+
const contextPath = path.resolve(__dirname, './..').replace(/\\/g, '/');
12+
const filePath = path.join(contextPath, 'main').replace(/\\/g, '/');
913

1014
describe('Builder', () => {
1115

@@ -21,7 +25,7 @@ describe('Builder', () => {
2125
expect(builder.machine instanceof Machine).toBeTruthy();
2226
builder.machine.generateLineControlStatements = true;
2327
expect(builder.machine.execute('@{__FILE__}:@{__LINE__}'))
24-
.toBe('#line 1 "main"\nmain:1');
28+
.toBe('#line 1 "' + filePath + '"\nmain:1');
2529
});
2630

2731
it('should execute "escape" filter', () => {

Diff for: spec/Include/IncludePath.spec.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// MIT License
2+
//
3+
// Copyright 2020 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
25+
'use strict';
26+
27+
const Builder = require('../../src');
28+
const backslashToSlash = require('../backslashToSlash');
29+
const Log = require('log');
30+
const path = require('path');
31+
const fs = require('fs');
32+
33+
const LOCAL_REPO_NOT_DEFINED_MESSAGE = "SPEC_GIT_LOCAL_REPO_PATH is not defined. Test will be skipped";
34+
35+
describe('__PATH__ variable - ', () => {
36+
37+
let builder;
38+
const contextPath = path.resolve(__dirname + "/../fixtures/lib/").replace(/\\/g, '/');
39+
40+
beforeEach(() => {
41+
builder = new Builder();
42+
builder.machine.path = contextPath;
43+
builder.machine.readers.github.username = process.env.SPEC_GITHUB_USERNAME;
44+
builder.machine.readers.github.token = process.env.SPEC_GITHUB_PASSWORD || process.env.SPEC_GITHUB_TOKEN;
45+
builder.logger = new Log(process.env.SPEC_LOGLEVEL || 'error');
46+
});
47+
48+
it('__PATH__ should be a local path', () => {
49+
let output = builder.machine.execute(`@include "${backslashToSlash(__dirname)}/../fixtures/lib/path.builder"`);
50+
expect(output).toContain(contextPath + "#path.builder@1");
51+
});
52+
53+
it('__PATH__ should be a git-local path', () => {
54+
if (process.env.SPEC_GIT_LOCAL_REPO_PATH != undefined) {
55+
let output = builder.machine.execute(`@include "git-local:${process.env.SPEC_GIT_LOCAL_REPO_PATH}/spec/fixtures/lib/path.builder@develop"`);
56+
expect(output).toContain("git-local:" + backslashToSlash(process.env.SPEC_GIT_LOCAL_REPO_PATH) + "/spec/fixtures/lib#path.builder@1");
57+
} else {
58+
console.log(LOCAL_REPO_NOT_DEFINED_MESSAGE);
59+
}
60+
});
61+
62+
it('__PATH__ should be a remote repository path', () => {
63+
let output = builder.machine.execute(`@include "github:electricimp/Builder/spec/fixtures/lib/path.builder@develop"`);
64+
expect(output).toContain('github:electricimp/Builder/spec/fixtures/lib#path.builder@1\n');
65+
});
66+
67+
it('__PATH__ should be a web link', () => {
68+
let output = builder.machine.execute(`@include "https://raw.githubusercontent.com/electricimp/Builder/develop/spec/fixtures/lib/path.builder"`);
69+
expect(output).toContain('https://raw.githubusercontent.com/electricimp/Builder/develop/spec/fixtures/lib#path.builder@1\n');
70+
});
71+
});

Diff for: spec/Include/IncludeRelativeLocal.spec.js

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// MIT License
2+
//
3+
// Copyright 2020 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
25+
'use strict';
26+
27+
const Builder = require('../../src');
28+
const backslashToSlash = require('../backslashToSlash');
29+
const Log = require('log');
30+
const path = require('path');
31+
const fs = require('fs');
32+
33+
describe('Builder is called for file in included directory - ', () => {
34+
35+
let builder;
36+
const contextPath = path.resolve(__dirname + "/../fixtures/include/sample-1/").replace(/\\/g, '/');
37+
38+
beforeEach(() => {
39+
builder = new Builder();
40+
builder.machine.path = contextPath;
41+
builder.machine.readers.file.runDir = contextPath;
42+
builder.machine.readers.file.inputFileDir = path.join(contextPath + '/dirZ');
43+
builder.logger = new Log(process.env.SPEC_LOGLEVEL || 'error');
44+
});
45+
46+
it('should search Y file in directory where X file located', () => {
47+
let output = builder.machine.execute(`@include "dirZ/file_case1.nut"`);
48+
expect(output).toContain('// y.nut (case 1)\n');
49+
});
50+
51+
it('should search Y file in directory where processing file located', () => {
52+
let output = builder.machine.execute(`@include "dirZ/file_case2.nut"`);
53+
expect(output).toContain('// y.nut (case 2)\n');
54+
});
55+
56+
it('should search Y file in directory where builder called', () => {
57+
let output = builder.machine.execute(`@include "dirZ/file_case3.nut"`);
58+
expect(output).toContain('// y.nut (case 3)\n');
59+
});
60+
61+
it('file not found', () => {
62+
const filePath = path.join(contextPath, 'dirX/x_case4.nut').replace(/\\/g, '/');
63+
const fileNotFoundMessage = `Local file "dirD/y4.nut" not found (${filePath}:1)`;
64+
try {
65+
builder.machine.execute(`@include "dirZ/file_case4.nut"`);
66+
fail();
67+
} catch (e) {
68+
expect(backslashToSlash(e.message)).toEqual(fileNotFoundMessage);
69+
}
70+
});
71+
});
72+
73+
describe('Builder is called for file in current directory - ', () => {
74+
75+
let builder;
76+
const contextPath = path.resolve(__dirname + "/../fixtures/include/sample-1/dirZ").replace(/\\/g, '/');
77+
78+
beforeEach(() => {
79+
builder = new Builder();
80+
builder.machine.path = contextPath;
81+
builder.machine.readers.file.runDir = contextPath;
82+
builder.machine.readers.file.inputFileDir = path.join(contextPath);
83+
builder.logger = new Log(process.env.SPEC_LOGLEVEL || 'error');
84+
});
85+
86+
it('should search Y file in directory where X file located', () => {
87+
let output = builder.machine.execute(`@include "file_case1.nut"`);
88+
expect(output).toContain('// y.nut (case 1)\n');
89+
});
90+
91+
it('should search Y file in directory where processing file located', () => {
92+
let output = builder.machine.execute(`@include "file_case2.nut"`);
93+
expect(output).toContain('// y.nut (case 2)\n');
94+
});
95+
96+
it('should search Y file in directory where builder called', () => {
97+
const filePath = path.join(contextPath, '/../dirX/x_case3.nut').replace(/\\/g, '/');
98+
const fileNotFoundMessage = `Local file "dirD/y3.nut" not found (${filePath}:1)`;
99+
try {
100+
builder.machine.execute(`@include "file_case3.nut"`);
101+
fail();
102+
} catch (e) {
103+
expect(backslashToSlash(e.message)).toEqual(fileNotFoundMessage);
104+
}
105+
});
106+
});
107+
108+
describe('Builder is called for file in deep included directory - ', () => {
109+
110+
let builder;
111+
const contextPath = path.resolve(__dirname + "/../fixtures/include/").replace(/\\/g, '/');
112+
113+
beforeEach(() => {
114+
builder = new Builder();
115+
builder.machine.path = contextPath;
116+
builder.machine.readers.file.runDir = contextPath;
117+
builder.machine.readers.file.inputFileDir = path.join(contextPath + '/sample-1/dirZ');
118+
builder.logger = new Log(process.env.SPEC_LOGLEVEL || 'error');
119+
});
120+
121+
it('should search Y file in directory where X file located', () => {
122+
let output = builder.machine.execute(`@include "sample-1/dirZ/file_case1.nut"`);
123+
expect(output).toContain('// y.nut (case 1)\n');
124+
});
125+
126+
it('should search Y file in directory where processing file located', () => {
127+
let output = builder.machine.execute(`@include "sample-1/dirZ/file_case2.nut"`);
128+
expect(output).toContain('// y.nut (case 2)\n');
129+
});
130+
131+
it('file not found', () => {
132+
const filePath = path.join(contextPath, 'sample-1/dirX/x_case3.nut').replace(/\\/g, '/');
133+
const fileNotFoundMessage = `Local file "dirD/y3.nut" not found (${filePath}:1)`;
134+
try {
135+
builder.machine.execute(`@include "sample-1/dirZ/file_case3.nut"`);
136+
fail();
137+
} catch (e) {
138+
expect(backslashToSlash(e.message)).toEqual(fileNotFoundMessage);
139+
}
140+
});
141+
142+
it('should search Y file in directory where builder called', () => {
143+
let output = builder.machine.execute(`@include "sample-1/dirZ/file_case4.nut"`);
144+
expect(output).toContain('// y.nut (case 4)\n');
145+
});
146+
});

0 commit comments

Comments
 (0)