Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 9a5e140

Browse files
committed
Refactor tests to get more Mix project tests
1 parent 8248559 commit 9a5e140

File tree

10 files changed

+187
-97
lines changed

10 files changed

+187
-97
lines changed

lib/init.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,12 @@ const handleResult = async (compileResult, filePath) => {
257257
const errorStack = await parseError(resultString, filePath);
258258
const warningStack = await parseWarning(resultString, filePath);
259259
const legacyWarningStack = await parseLegacyWarning(resultString, filePath);
260-
return errorStack.concat(warningStack).concat(legacyWarningStack)
260+
261+
const results = errorStack.concat(warningStack).concat(legacyWarningStack)
261262
.filter(error => error !== null)
262263
.map(error => error);
264+
265+
return results;
263266
} catch (Error) {
264267
// eslint-disable-next-line no-console
265268
console.error('linter-elixirc:', Error);
@@ -310,7 +313,17 @@ const lintElixirc = async (textEditor) => {
310313

311314
const fileText = textEditor.getText();
312315
const execOpts = await getOpts(filePath);
316+
317+
const argsAsString = elixircArgs.join(' ');
318+
const optsAsString = JSON.stringify(execOpts);
319+
// eslint-disable-next-line no-console
320+
console.log(`Executing the command: ${elixircPath} ${argsAsString} ${optsAsString}`);
321+
313322
const result = await exec(elixircPath, elixircArgs, execOpts);
323+
const str = JSON.stringify(result, null, 2);
324+
// eslint-disable-next-line no-console
325+
console.log(`Result: ${str}`);
326+
314327
// Cleanup the temp dir
315328
tempDir.removeCallback();
316329
if (textEditor.getText() !== fileText) {
@@ -324,7 +337,15 @@ const lintMix = async (textEditor) => {
324337
const filePath = textEditor.getPath();
325338
const fileText = textEditor.getText();
326339
const execOpts = await getOpts(filePath);
340+
341+
const optsAsString = JSON.stringify(execOpts);
342+
// eslint-disable-next-line no-console
343+
console.log(`Executing the command: ${mixPath} compile ${optsAsString}`);
344+
327345
const result = await exec(mixPath, ['compile'], execOpts);
346+
const str = JSON.stringify(result, null, 4);
347+
// eslint-disable-next-line no-console
348+
console.log(`Result: ${str}`);
328349
if (textEditor.getText() !== fileText) {
329350
// File contents have changed since the run was triggered, don't update messages
330351
return null;
@@ -371,12 +392,20 @@ export default {
371392
name: 'Elixir',
372393
async lint(textEditor) {
373394
const filePath = textEditor.getPath();
395+
// const forcedElixirc = isForcedElixirc();
396+
// const mixProject = await isMixProject(filePath);
397+
// const exsFile = isExsFile(filePath);
398+
// const phoenixProject = (await isPhoenixProject());
374399
if (
375400
isForcedElixirc() ||
376401
!(await isMixProject(filePath)) ||
377402
isExsFile(filePath) ||
378403
(await isPhoenixProject(filePath))
379404
) {
405+
// forcedElixirc ||
406+
// !mixProject ||
407+
// exsFile ||
408+
// phoenixProject) {
380409
return lintElixirc(textEditor);
381410
}
382411
return lintMix(textEditor);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Single line error, still mode 2
2+
defmodule Identicon do
3+
def main(input) do
4+
input
5+
|> hash_input
6+
end
7+
8+
def has_input(input) do
9+
hex = :crypto.has(:md5, input)
10+
|> :binary.bin_to_list
11+
12+
%Identicon.Image{hex: hex}
13+
end
14+
end

spec/fixtures/mix-proj/lib/script.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule Script do
2+
defp simple_function do
3+
:ok
4+
end
5+
end
File renamed without changes.

spec/linter-elixirc-spec.js

Lines changed: 138 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,147 @@
11
'use babel';
22

33
import { join } from 'path';
4+
// eslint-disable-next-line import/no-extraneous-dependencies
45
import { remove } from 'fs-extra';
56

6-
const validPath = join(__dirname, 'fixtures', 'valid.ex');
7-
const warningPath = join(__dirname, 'fixtures', 'proj', 'lib', 'proj.ex');
8-
const errorMode1Path = join(__dirname, 'fixtures', 'error-mode1.ex');
9-
const errorMode2Path = join(__dirname, 'fixtures', 'error-mode2.ex');
10-
const exsFilePath = join(__dirname, 'fixtures', 'script.exs');
7+
const validPathElixirc = join(__dirname, 'fixtures', 'elixirc', 'valid.ex');
8+
const warningPathElixirc = join(__dirname, 'fixtures', 'elixirc', 'warning.ex');
9+
const errorMode1PathElixirc = join(__dirname, 'fixtures', 'elixirc', 'error-mode1.ex');
10+
const errorMode2PathElixirc = join(__dirname, 'fixtures', 'elixirc', 'error-mode2.ex');
11+
const exsFilePathElixirc = join(__dirname, 'fixtures', 'elixirc', 'script.exs');
1112

12-
const mixBuildDirectory = join(__dirname, 'fixtures', 'proj', '_build');
13+
const validPathMix = join(__dirname, 'fixtures', 'mix-proj', 'lib', 'valid.ex');
14+
const warningPathMix = join(__dirname, 'fixtures', 'mix-proj', 'lib', 'warning.ex');
15+
const errorMode1PathMix = join(__dirname, 'fixtures', 'mix-proj', 'lib', 'error-mode1.ex');
16+
const errorMode2PathMix = join(__dirname, 'fixtures', 'mix-proj', 'lib', 'error-mode2.ex');
17+
const exsFilePathMix = join(__dirname, 'fixtures', 'mix-proj', 'lib', 'script.exs');
18+
19+
const mixBuildDirectory = join(__dirname, 'fixtures', 'mix-proj', '_build');
1320
remove(mixBuildDirectory);
1421

1522
describe('The elixirc provider for Linter', () => {
16-
describe('when using the standard configuration', () => {
17-
let lint;
18-
19-
beforeEach(() => {
20-
atom.config.set('linter-elixirc.forceElixirc', false);
21-
lint = require('../lib/init.js').provideLinter().lint;
22-
atom.workspace.destroyActivePaneItem();
23-
24-
waitsForPromise(() =>
25-
Promise.all([
26-
atom.packages.activatePackage('linter-elixirc'),
27-
atom.packages.activatePackage('language-elixir'),
28-
]),
29-
);
30-
});
31-
32-
it('works with mode 1 errors', () => {
33-
waitsForPromise(() =>
34-
atom.workspace.open(errorMode1Path).then(editor => lint(editor)).then((messages) => {
35-
expect(messages.length).toBe(1);
36-
expect(messages[0].severity).toBe('error');
37-
expect(messages[0].html).not.toBeDefined();
38-
expect(messages[0].excerpt).toBe('(ArgumentError) Dangerous is not available');
39-
expect(messages[0].location.file).toBe(errorMode1Path);
40-
expect(messages[0].location.position).toEqual([[1, 0], [1, 32]]);
41-
}),
42-
);
43-
});
44-
45-
it('works with mode 1 errors for elixirc', () => {
46-
waitsForPromise(() =>
47-
atom.workspace.open(errorMode1Path).then(editor => lint(editor)).then((messages) => {
48-
expect(messages.length).toBe(1);
49-
expect(messages[0].severity).toBe('error');
50-
expect(messages[0].html).not.toBeDefined();
51-
expect(messages[0].excerpt).toBe('(ArgumentError) Dangerous is not available');
52-
expect(messages[0].location.file).toBe(errorMode1Path);
53-
expect(messages[0].location.position).toEqual([[1, 0], [1, 32]]);
54-
}),
55-
);
56-
});
57-
58-
it('works with mode 2 errors', () => {
59-
waitsForPromise(() =>
60-
atom.workspace.open(errorMode2Path).then(editor => lint(editor)).then((messages) => {
61-
expect(messages.length).toBe(1);
62-
expect(messages[0].severity).toBe('error');
63-
expect(messages[0].html).not.toBeDefined();
64-
expect(messages[0].excerpt).toBe('(CompileError) module Usefulness is not loaded and could not be found');
65-
expect(messages[0].location.file).toBe(errorMode2Path);
66-
expect(messages[0].location.position).toEqual([[3, 2], [3, 20]]);
67-
}),
68-
);
69-
});
70-
71-
it('works with warnings', () => {
72-
waitsForPromise(() =>
73-
atom.workspace.open(warningPath).then(editor => lint(editor)).then((messages) => {
74-
expect(messages.length).toBe(1);
75-
expect(messages[0].severity).toBe('warning');
76-
expect(messages[0].html).not.toBeDefined();
77-
expect(messages[0].excerpt).toBe('variable "prepare_for_call" does not exist and is being expanded to "prepare_for_call()", please use parentheses to remove the ambiguity or change the variable name');
78-
expect(messages[0].location.file).toBe(warningPath);
79-
expect(messages[0].location.position).toEqual([[20, 4], [20, 20]]);
80-
}),
81-
);
82-
});
83-
84-
it('works with .exs files', () => {
85-
waitsForPromise(() =>
86-
atom.workspace.open(exsFilePath).then(editor => lint(editor)).then((messages) => {
87-
expect(messages.length).toBe(1);
88-
expect(messages[0].severity).toBe('warning');
89-
expect(messages[0].html).not.toBeDefined();
90-
expect(messages[0].excerpt).toBe('function simple_function/0 is unused');
91-
expect(messages[0].location.file).toBe(exsFilePath);
92-
expect(messages[0].location.position).toEqual([[1, 2], [1, 25]]);
93-
}),
94-
);
23+
describe('when not working inside a Mix project', () => {
24+
describe('and using the standard configuration', () => {
25+
let lint;
26+
27+
beforeEach(() => {
28+
lint = require('../lib/init.js').provideLinter().lint;
29+
atom.workspace.destroyActivePaneItem();
30+
31+
waitsForPromise(() =>
32+
Promise.all([
33+
atom.packages.activatePackage('linter-elixirc'),
34+
atom.packages.activatePackage('language-elixir'),
35+
]),
36+
);
37+
});
38+
39+
it('works with mode 1 errors', () => {
40+
waitsForPromise(() =>
41+
atom.workspace.open(errorMode1PathElixirc).then(editor =>
42+
lint(editor)).then((messages) => {
43+
expect(messages.length).toBe(1);
44+
expect(messages[0].severity).toBe('error');
45+
expect(messages[0].html).not.toBeDefined();
46+
expect(messages[0].excerpt).toBe('(ArgumentError) Dangerous is not available');
47+
expect(messages[0].location.file).toBe(errorMode1PathElixirc);
48+
expect(messages[0].location.position).toEqual([[1, 0], [1, 32]]);
49+
}),
50+
);
51+
});
52+
53+
it('works with mode 2 errors', () => {
54+
waitsForPromise(() =>
55+
atom.workspace.open(errorMode2PathElixirc).then(editor =>
56+
lint(editor)).then((messages) => {
57+
expect(messages.length).toBe(1);
58+
expect(messages[0].severity).toBe('error');
59+
expect(messages[0].html).not.toBeDefined();
60+
expect(messages[0].excerpt).toBe('(CompileError) module Usefulness is not loaded and could not be found');
61+
expect(messages[0].location.file).toBe(errorMode2PathElixirc);
62+
expect(messages[0].location.position).toEqual([[3, 2], [3, 20]]);
63+
}),
64+
);
65+
});
66+
67+
it('works with warnings', () => {
68+
waitsForPromise(() =>
69+
atom.workspace.open(warningPathElixirc).then(editor => lint(editor)).then((messages) => {
70+
expect(messages.length).toBe(1);
71+
expect(messages[0].severity).toBe('warning');
72+
expect(messages[0].html).not.toBeDefined();
73+
expect(messages[0].excerpt).toBe('variable "prepare_for_call" does not exist and is being expanded to "prepare_for_call()", please use parentheses to remove the ambiguity or change the variable name');
74+
expect(messages[0].location.file).toBe(warningPathElixirc);
75+
expect(messages[0].location.position).toEqual([[20, 4], [20, 20]]);
76+
}),
77+
);
78+
});
79+
80+
it('works with .exs files', () => {
81+
waitsForPromise(() =>
82+
atom.workspace.open(exsFilePathElixirc).then(editor => lint(editor)).then((messages) => {
83+
expect(messages.length).toBe(1);
84+
expect(messages[0].severity).toBe('warning');
85+
expect(messages[0].html).not.toBeDefined();
86+
expect(messages[0].excerpt).toBe('function simple_function/0 is unused');
87+
expect(messages[0].location.file).toBe(exsFilePathElixirc);
88+
expect(messages[0].location.position).toEqual([[1, 2], [1, 25]]);
89+
}),
90+
);
91+
});
92+
93+
it('finds nothing wrong with a valid file', () => {
94+
waitsForPromise(() =>
95+
atom.workspace.open(validPathElixirc).then(editor => lint(editor)).then((messages) => {
96+
expect(messages.length).toBe(0);
97+
}),
98+
);
99+
});
95100
});
101+
});
96102

97-
it('finds nothing wrong with a valid file', () => {
98-
waitsForPromise(() =>
99-
atom.workspace.open(validPath).then(editor => lint(editor)).then((messages) => {
100-
expect(messages.length).toBe(0);
101-
}),
102-
);
103+
describe('when working inside a Mix project', () => {
104+
describe('and using the standard configuration', () => {
105+
let lint;
106+
107+
beforeEach(() => {
108+
lint = require('../lib/init.js').provideLinter().lint;
109+
atom.workspace.destroyActivePaneItem();
110+
111+
waitsForPromise(() =>
112+
Promise.all([
113+
atom.packages.activatePackage('linter-elixirc'),
114+
atom.packages.activatePackage('language-elixir'),
115+
]),
116+
);
117+
});
118+
119+
it('works with mode 2 errors', () => {
120+
waitsForPromise(() =>
121+
atom.workspace.open(errorMode2PathMix).then(editor =>
122+
lint(editor)).then((messages) => {
123+
expect(messages.length).toBe(1);
124+
expect(messages[0].severity).toBe('error');
125+
expect(messages[0].html).not.toBeDefined();
126+
expect(messages[0].excerpt).toBe('(CompileError) Identicon.Image.__struct__/1 is undefined, cannot expand struct Identicon.Image');
127+
expect(messages[0].location.file).toBe(errorMode2PathMix);
128+
expect(messages[0].location.position).toEqual([[11, 4], [11, 30]]);
129+
}),
130+
);
131+
});
132+
133+
it('works with .exs files', () => {
134+
waitsForPromise(() =>
135+
atom.workspace.open(exsFilePathMix).then(editor => lint(editor)).then((messages) => {
136+
expect(messages.length).toBe(1);
137+
expect(messages[0].severity).toBe('warning');
138+
expect(messages[0].html).not.toBeDefined();
139+
expect(messages[0].excerpt).toBe('function simple_function/0 is unused');
140+
expect(messages[0].location.file).toBe(exsFilePathMix);
141+
expect(messages[0].location.position).toEqual([[1, 2], [1, 25]]);
142+
}),
143+
);
144+
});
103145
});
104146
});
105147
});
@@ -122,13 +164,13 @@ describe('when using the setting forceElixirc', () => {
122164

123165
it('works with warnings', () => {
124166
waitsForPromise(() =>
125-
atom.workspace.open(warningPath).then(editor => lint(editor)).then((messages) => {
167+
atom.workspace.open(errorMode2PathMix).then(editor => lint(editor)).then((messages) => {
126168
expect(messages.length).toBe(1);
127-
expect(messages[0].severity).toBe('warning');
169+
expect(messages[0].severity).toBe('error');
128170
expect(messages[0].html).not.toBeDefined();
129-
expect(messages[0].excerpt).toBe('variable "prepare_for_call" does not exist and is being expanded to "prepare_for_call()", please use parentheses to remove the ambiguity or change the variable name');
130-
expect(messages[0].location.file).toBe(warningPath);
131-
expect(messages[0].location.position).toEqual([[20, 4], [20, 20]]);
171+
expect(messages[0].excerpt).toBe('(CompileError) Identicon.Image.__struct__/1 is undefined, cannot expand struct Identicon.Image');
172+
expect(messages[0].location.file).toBe(errorMode2PathMix);
173+
expect(messages[0].location.position).toEqual([[11, 4], [11, 30]]);
132174
}),
133175
);
134176
});

0 commit comments

Comments
 (0)