Skip to content

Commit 0eef31c

Browse files
committed
Adding test cases
1 parent 99ea368 commit 0eef31c

File tree

7 files changed

+649
-24
lines changed

7 files changed

+649
-24
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,47 @@ npm i @scalvert/ember-template-lint-formatter-sonarqube --save-dev
1919
yarn add @scalvert/ember-template-lint-formatter-sonarqube --dev
2020
```
2121

22+
Output will be in 'Generic Issue Format'.
23+
24+
```json
25+
{
26+
"issues": [
27+
{
28+
"engineId": "ember-template-lint",
29+
"ruleId": "no-bare-strings",
30+
"severity": "MINOR",
31+
"type": "CODE_SMELL",
32+
"primaryLocation": {
33+
"message": "Non-translated string used",
34+
"filePath": "app/templates/application.hbs",
35+
"textRange": {
36+
"startLine": 1,
37+
"startColumn": 4,
38+
"endLine": 1,
39+
"endColumn": 14
40+
}
41+
}
42+
},
43+
{
44+
"engineId": "ember-template-lint",
45+
"ruleId": "no-bare-strings",
46+
"severity": "MINOR",
47+
"type": "CODE_SMELL",
48+
"primaryLocation": {
49+
"message": "Non-translated string used",
50+
"filePath": "app/templates/application.hbs",
51+
"textRange": {
52+
"startLine": 1,
53+
"startColumn": 25,
54+
"endLine": 1,
55+
"endColumn": 48
56+
}
57+
}
58+
}
59+
]
60+
}
61+
```
62+
2263
## Usage
2364

2465
```shell
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
const Project = require('fixturify-project');
6+
7+
const ROOT = process.cwd();
8+
9+
// this is the default .editorconfig file for new ember-cli apps, taken from:
10+
// https://github.com/ember-cli/ember-new-output/blob/stable/.editorconfig
11+
const DEFAULT_EDITOR_CONFIG = `
12+
# EditorConfig helps developers define and maintain consistent
13+
# coding styles between different editors and IDEs
14+
# editorconfig.org
15+
16+
root = true
17+
18+
[*]
19+
end_of_line = lf
20+
charset = utf-8
21+
trim_trailing_whitespace = true
22+
insert_final_newline = true
23+
indent_style = space
24+
indent_size = 2
25+
26+
[*.hbs]
27+
insert_final_newline = false
28+
29+
[*.{diff,md}]
30+
trim_trailing_whitespace = false
31+
`;
32+
33+
// this is the default .template-lintrc.js used by ember-cli apps, taken from:
34+
// https://github.com/ember-cli/ember-new-output/blob/stable/.template-lintrc.js
35+
const DEFAULT_TEMPLATE_LINTRC = `
36+
'use strict';
37+
38+
module.exports = {
39+
extends: 'recommended'
40+
};
41+
`;
42+
43+
module.exports = class FakeProject extends Project {
44+
static defaultSetup() {
45+
let project = new this();
46+
47+
project.files['.template-lintrc.js'] = DEFAULT_TEMPLATE_LINTRC;
48+
project.files['.editorconfig'] = DEFAULT_EDITOR_CONFIG;
49+
50+
project.writeSync();
51+
52+
return project;
53+
}
54+
55+
constructor(name = 'fake-project', ...arguments_) {
56+
super(name, ...arguments_);
57+
}
58+
59+
setConfig(config) {
60+
let configFileContents =
61+
config === undefined
62+
? DEFAULT_TEMPLATE_LINTRC
63+
: `module.exports = ${JSON.stringify(config, null, 2)};`;
64+
65+
this.files['.template-lintrc.js'] = configFileContents;
66+
67+
this.writeSync();
68+
}
69+
70+
getConfig() {
71+
return require(path.join(this.baseDir, '.template-lintrc'));
72+
}
73+
74+
setEditorConfig(value = DEFAULT_EDITOR_CONFIG) {
75+
this.files['.editorconfig'] = value;
76+
77+
this.writeSync();
78+
}
79+
80+
path(subPath) {
81+
return subPath ? path.join(this.baseDir, subPath) : this.baseDir;
82+
}
83+
84+
// behave like a TempDir from broccoli-test-helper
85+
// eslint-disable-next-line unicorn/prevent-abbreviations
86+
write(dirJSON) {
87+
Object.assign(this.files, dirJSON);
88+
this.writeSync();
89+
}
90+
91+
chdir() {
92+
this._dirChanged = true;
93+
94+
// ensure the directory structure is created initially
95+
this.writeSync();
96+
97+
process.chdir(this.baseDir);
98+
}
99+
100+
dispose() {
101+
if (this._dirChanged) {
102+
process.chdir(ROOT);
103+
}
104+
105+
return super.dispose();
106+
}
107+
};

__tests__/index-test.js

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
const execa = require("execa");
12
const stripAnsi = require("strip-ansi");
3+
const Project = require("./__utils__/fake-project");
24
const SonarQubeFormatter = require("../index");
35

46
class MockConsole {
@@ -20,18 +22,181 @@ class MockConsole {
2022
}
2123

2224
describe("SonarQube Formatter", () => {
25+
let project;
26+
27+
beforeEach(() => {
28+
project = new Project("fake-project");
29+
});
30+
31+
afterEach(() => {
32+
// project.dispose();
33+
});
34+
2335
it("can format output from no results", () => {
2436
let mockConsole = new MockConsole();
2537
let formatter = new SonarQubeFormatter({
2638
console: mockConsole,
2739
});
2840

29-
formatter.print([]);
41+
formatter.print({});
3042

3143
expect(mockConsole.toString()).toMatchInlineSnapshot(`
3244
"{
3345
\\"issues\\": []
3446
}"
3547
`);
3648
});
49+
50+
it("can format output when there are warnings", async () => {
51+
project.setConfig({
52+
rules: {
53+
"no-bare-strings": "warn",
54+
},
55+
});
56+
project.write({
57+
app: {
58+
templates: {
59+
"application.hbs":
60+
"<h2>Here too!!</h2> <div>Bare strings are bad...</div>",
61+
components: {
62+
"foo.hbs": "{{fooData}}",
63+
},
64+
},
65+
},
66+
});
67+
68+
let result = await emberTemplateLint();
69+
70+
expect(result.stdout).toMatchInlineSnapshot(`
71+
"{
72+
\\"issues\\": [
73+
{
74+
\\"engineId\\": \\"ember-template-lint\\",
75+
\\"ruleId\\": \\"no-bare-strings\\",
76+
\\"severity\\": \\"MINOR\\",
77+
\\"type\\": \\"CODE_SMELL\\",
78+
\\"primaryLocation\\": {
79+
\\"message\\": \\"Non-translated string used\\",
80+
\\"filePath\\": \\"app/templates/application.hbs\\",
81+
\\"textRange\\": {
82+
\\"startLine\\": 1,
83+
\\"startColumn\\": 4,
84+
\\"endLine\\": 1,
85+
\\"endColumn\\": 14
86+
}
87+
}
88+
},
89+
{
90+
\\"engineId\\": \\"ember-template-lint\\",
91+
\\"ruleId\\": \\"no-bare-strings\\",
92+
\\"severity\\": \\"MINOR\\",
93+
\\"type\\": \\"CODE_SMELL\\",
94+
\\"primaryLocation\\": {
95+
\\"message\\": \\"Non-translated string used\\",
96+
\\"filePath\\": \\"app/templates/application.hbs\\",
97+
\\"textRange\\": {
98+
\\"startLine\\": 1,
99+
\\"startColumn\\": 25,
100+
\\"endLine\\": 1,
101+
\\"endColumn\\": 48
102+
}
103+
}
104+
}
105+
]
106+
}"
107+
`);
108+
});
109+
110+
it("can format output when there are errors", async () => {
111+
project.setConfig({
112+
rules: {
113+
"no-bare-strings": "error",
114+
},
115+
});
116+
project.write({
117+
app: {
118+
templates: {
119+
"application.hbs":
120+
"<h2>Here too!!</h2> <div>Bare strings are bad...</div>",
121+
components: {
122+
"foo.hbs": "{{fooData}}",
123+
},
124+
},
125+
},
126+
});
127+
128+
let result = await emberTemplateLint();
129+
130+
expect(result.stdout).toMatchInlineSnapshot(`
131+
"{
132+
\\"issues\\": [
133+
{
134+
\\"engineId\\": \\"ember-template-lint\\",
135+
\\"ruleId\\": \\"no-bare-strings\\",
136+
\\"severity\\": \\"CRITICAL\\",
137+
\\"type\\": \\"BUG\\",
138+
\\"primaryLocation\\": {
139+
\\"message\\": \\"Non-translated string used\\",
140+
\\"filePath\\": \\"app/templates/application.hbs\\",
141+
\\"textRange\\": {
142+
\\"startLine\\": 1,
143+
\\"startColumn\\": 4,
144+
\\"endLine\\": 1,
145+
\\"endColumn\\": 14
146+
}
147+
}
148+
},
149+
{
150+
\\"engineId\\": \\"ember-template-lint\\",
151+
\\"ruleId\\": \\"no-bare-strings\\",
152+
\\"severity\\": \\"CRITICAL\\",
153+
\\"type\\": \\"BUG\\",
154+
\\"primaryLocation\\": {
155+
\\"message\\": \\"Non-translated string used\\",
156+
\\"filePath\\": \\"app/templates/application.hbs\\",
157+
\\"textRange\\": {
158+
\\"startLine\\": 1,
159+
\\"startColumn\\": 25,
160+
\\"endLine\\": 1,
161+
\\"endColumn\\": 48
162+
}
163+
}
164+
}
165+
]
166+
}"
167+
`);
168+
});
169+
170+
function emberTemplateLint(argumentsOrOptions, options) {
171+
if (arguments.length > 0) {
172+
if (arguments.length === 1) {
173+
options = Array.isArray(argumentsOrOptions) ? {} : argumentsOrOptions;
174+
}
175+
} else {
176+
argumentsOrOptions = [];
177+
options = {};
178+
}
179+
180+
const mergedOptions = Object.assign(
181+
{
182+
reject: false,
183+
cwd: project.baseDir,
184+
},
185+
options
186+
);
187+
188+
return execa(
189+
process.execPath,
190+
[
191+
require.resolve(
192+
"../node_modules/ember-template-lint/bin/ember-template-lint.js"
193+
),
194+
".",
195+
"--format",
196+
require.resolve(".."),
197+
...argumentsOrOptions,
198+
],
199+
mergedOptions
200+
);
201+
}
37202
});

0 commit comments

Comments
 (0)