Completed
Push — master ( a923ac...ad2d91 )
by Jean
29s
created

configure.test.js ➔ ... ➔ .close   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 1
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 1
loc 1
rs 10
c 0
b 0
f 0
1
import chai from 'chai';
2
import chaiaspromised from 'chai-as-promised';
3
import mockfs from 'mock-fs';
4
import sinon from 'sinon';
5
import sinonchai from 'sinon-chai';
6
import mute from 'mute';
7
8
import readline from 'readline';
9
10
import configure from '../src/configure.js';
11
12
let expect = chai.expect;
13
chai.use(chaiaspromised);
14
chai.use(sinonchai);
15
16
describe(`[module] configure`, function() {
17
	describe(`[method] load`, function() {
18
		let defaultconfpath = `.codingamerc`;
19
		let incorrectconfpath = `.codingamerc.txt`;
20
		let defaultconf = {
21
			"username": `me`,
22
			"password": `somepass`,
23
			"exercise": `5711567e959cf54dd2dd79c1b4c259560d6ba46`,
24
			"tests": [1, 2],
25
			"language": `Python`,
26
			"bundle": `bundle.py`
27
		};
28
		before(function() {
29
			mockfs({
30
				[defaultconfpath]: JSON.stringify(defaultconf),
31
				[incorrectconfpath]: `This is not valid JSON!`
32
			});
33
		});
34
		after(function() {
35
			mockfs.restore();
36
		});
37
		afterEach(function() {
38
			for (let param in defaultconf) {
39
				configure.forget(param);
40
			}
41
		});
42
43
		it(`should return the content of file parsed as JSON`, function() {
44
			let conf = configure.load(defaultconfpath);
45
			return expect(conf).to.eventually.be.deep.equal(defaultconf);
46
		});
47
48
		it(`should reject because of incorrect file path`, function() {
49
			let conf = configure.load(`/incorrect/path/.codingamerc`);
50
			return expect(conf).to.be.rejectedWith(Error);
51
		});
52
53
		it(`should reject because of incorrect parsing of JSON`, function() {
54
			let conf = configure.load(incorrectconfpath);
55
			return expect(conf).to.be.rejectedWith(Error);
56
		});
57
58
		it(`should return options if path is incorrect and options is defined`, function() {
59
			let conf = configure.load(undefined, defaultconf);
60
			return expect(conf).to.eventually.be.deep.equal(defaultconf);
61
		});
62
	});
63 View Code Duplication
	describe(`[method] get`, function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
64
		let filepath = `file.txt`;
65
		let filecontent = `Hello world!`;
66
		let shellcmd = [`echo`, filecontent];
67
		let defaultconf = {
68
			"shell": shellcmd,
69
			"wrongshell": [`notacommand`],
70
			"path": filepath,
71
			"notapath": `/not/a/path`
72
		};
73
		let answer = `42`;
74
		before(function(done) {
75
			mockfs({
76
				[filepath]: filecontent
77
			});
78
			configure.load(undefined, defaultconf).then(function() {
79
				done();
80
			});
81
		});
82
		after(function() {
83
			mockfs.restore();
84
			for (let param in defaultconf) {
85
				configure.forget(param);
86
			}
87
		});
88
		let sandbox;
89
		let createInterface;
90
		beforeEach(function() {
91
			sandbox = sinon.sandbox.create();
92
			createInterface = sandbox.stub(readline, `createInterface`).callsFake(function() {
93
				return {
94
					"question": function(question, cb) {
95
						cb(answer);
96
					},
97
					"close": function() {}
98
				};
99
			});
100
		});
101
		afterEach(function() {
102
			sandbox.restore();
103
		});
104
		it(`should return the string property as it is`, function() {
105
			let get = configure.get(`path`);
106
			return expect(get).to.eventually.be.equal(filepath);
107
		});
108
		it(`should return the array property as it is`, function() {
109
			let get = configure.get(`shell`);
110
			return expect(get).to.eventually.be.deep.equal(shellcmd);
111
		});
112
		it(`should return the result of the shell command`, function() {
113
			let get = configure.get(`shell`, `shell`);
114
			return expect(get).to.eventually.be.equal(filecontent);
115
		});
116
		it(`should return the content of the file`, function() {
117
			let get = configure.get(`path`, `file`);
118
			return expect(get).to.eventually.be.deep.equal({
119
				"path": filepath,
120
				"data": filecontent
121
			});
122
		});
123
		it(`should return answer to the question`, function() {
124
			let ask = `ask something?`;
125
			let get = configure.get(`questionproperty`, ``, ask);
126
			expect(createInterface).to.be.calledOnce;
0 ignored issues
show
introduced by
The result of the property access to expect(createInterface).to.be.calledOnce is not used.
Loading history...
127
			return expect(get).to.eventually.be.equal(answer);
128
		});
129
		it(`should reject because property doesn't exist`, function() {
130
			let get = configure.get(`notaproperty`);
131
			return expect(get).to.be.rejectedWith(Error);
132
		});
133
		it(`should reject if first parameter is not a string`, function() {
134
			let get = configure.get(42);
135
			return expect(get).to.be.rejectedWith(Error);
136
		});
137
		it(`should reject if shell command is failing`, function() {
138
			let unmute = mute(process.stderr);
139
			let get = configure.get(`wrongshell`, `shell`);
140
			get.catch(unmute);
141
			return expect(get).to.be.rejectedWith(Error);
142
		});
143
		it(`should reject if file doesn't exist`, function() {
144
			let unmute = mute(process.stderr);
145
			let get = configure.get(`notapath`, `file`);
146
			get.catch(unmute);
147
			return expect(get).to.be.rejectedWith(Error);
148
		});
149
		it(`should reject if question is not a string`, function() {
150
			let get = configure.get(`incorrectquestionproperty`, ``, 42);
151
			expect(createInterface).to.be.calledOnce;
0 ignored issues
show
introduced by
The result of the property access to expect(createInterface).to.be.calledOnce is not used.
Loading history...
152
			return expect(get).to.be.rejectedWith(Error);
153
		});
154
	});
155
	describe(`[method] forget`, function() {
156
		let conf = {
157
			"property": `value`
158
		};
159
		before(function() {
160
			configure.load(undefined, conf);
161
		});
162
		it(`should forget about the parameter`, function() {
163
			configure.forget(`property`);
164
			let get = configure.get(`property`);
165
			return expect(get).to.be.rejectedWith(Error);
166
		});
167
		it(`should throw an error when the property is not a string`, function() {
168
			let error = {};
169
			try {
170
				configure.forget(42);
171
			} catch(e) {
172
				error = e;
173
			}
174
			return expect(error).to.be.an(`error`);
175
		});
176
	});
177
});
178