@itentialopensource/adapter-git 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,318 @@
1
+ /* @copyright Itential, LLC 2019 (pre-modifications) */
2
+
3
+ // Set globals
4
+ /* global describe it log pronghornProps */
5
+ /* eslint no-unused-vars: warn */
6
+ /* eslint no-underscore-dangle: warn */
7
+
8
+ // include required items for testing & logging
9
+ const assert = require('assert');
10
+ const fs = require('fs');
11
+ const mocha = require('mocha');
12
+ const path = require('path');
13
+ const winston = require('winston');
14
+ const { expect } = require('chai');
15
+ const { use } = require('chai');
16
+ const td = require('testdouble');
17
+
18
+ const anything = td.matchers.anything();
19
+
20
+ // stub and attemptTimeout are used throughout the code so set them here
21
+ let logLevel = 'none';
22
+ const stub = true;
23
+ const isRapidFail = false;
24
+ const isSaveMockData = false;
25
+ const attemptTimeout = 10000;
26
+
27
+ // these variables can be changed to run in integrated mode so easier to set them here
28
+ // always check these in with bogus data!!!
29
+ const host = 'localhost';
30
+ const username = 'username';
31
+ const password = 'password';
32
+ const sslenable = false;
33
+ const sslinvalid = false;
34
+
35
+ // these are the adapter properties. You generally should not need to alter
36
+ // any of these after they are initially set up
37
+ global.pronghornProps = {
38
+ pathProps: {
39
+ encrypted: false
40
+ },
41
+ adapterProps: {
42
+ adapters: [{
43
+ id: 'Test-Git',
44
+ type: 'Git',
45
+ stub: true,
46
+ properties: {
47
+ authentication: {
48
+ username: 'username',
49
+ password: 'password'
50
+ }
51
+ }
52
+ }]
53
+ }
54
+ };
55
+
56
+ global.$HOME = `${__dirname}/../..`;
57
+
58
+ // set the log levels that Pronghorn uses, spam and trace are not defaulted in so without
59
+ // this you may error on log.trace calls.
60
+ const myCustomLevels = {
61
+ levels: {
62
+ spam: 6,
63
+ trace: 5,
64
+ debug: 4,
65
+ info: 3,
66
+ warn: 2,
67
+ error: 1,
68
+ none: 0
69
+ }
70
+ };
71
+
72
+ // need to see if there is a log level passed in
73
+ process.argv.forEach((val) => {
74
+ // is there a log level defined to be passed in?
75
+ if (val.indexOf('--LOG') === 0) {
76
+ // get the desired log level
77
+ const inputVal = val.split('=')[1];
78
+
79
+ // validate the log level is supported, if so set it
80
+ if (Object.hasOwnProperty.call(myCustomLevels.levels, inputVal)) {
81
+ logLevel = inputVal;
82
+ }
83
+ }
84
+ });
85
+
86
+ // need to set global logging
87
+ global.log = winston.createLogger({
88
+ level: logLevel,
89
+ levels: myCustomLevels.levels,
90
+ transports: [
91
+ new winston.transports.Console()
92
+ ]
93
+ });
94
+
95
+ /**
96
+ * Runs the common asserts for test
97
+ */
98
+ function runCommonAsserts(data, error) {
99
+ assert.equal(undefined, error);
100
+ assert.notEqual(undefined, data);
101
+ assert.notEqual(null, data);
102
+ assert.notEqual(undefined, data.response);
103
+ assert.notEqual(null, data.response);
104
+ }
105
+
106
+ const Git = require('../../adapter');
107
+
108
+ describe('[integration] Git Adapter Test', () => {
109
+ describe('Git Class Tests', () => {
110
+ const a = new Git(
111
+ pronghornProps.adapterProps.adapters[0].id,
112
+ pronghornProps.adapterProps.adapters[0].properties
113
+ );
114
+
115
+ if (isRapidFail) {
116
+ const state = {};
117
+ state.passed = true;
118
+
119
+ mocha.afterEach(function x() {
120
+ state.passed = state.passed
121
+ && (this.currentTest.state === 'passed');
122
+ });
123
+ mocha.beforeEach(function x() {
124
+ if (!state.passed) {
125
+ return this.currentTest.skip();
126
+ }
127
+ return true;
128
+ });
129
+ }
130
+
131
+ describe('#class instance created', () => {
132
+ it('should be a class with properties', (done) => {
133
+ assert.notEqual(null, a);
134
+ assert.notEqual(undefined, a);
135
+ const check = global.pronghornProps.adapterProps.adapters[0].id;
136
+ assert.equal(check, a.id);
137
+ done();
138
+ }).timeout(attemptTimeout);
139
+ });
140
+
141
+ describe('#connect', () => {
142
+ if (!stub) {
143
+ it('should get connected', (done) => {
144
+ a.connect();
145
+ assert.equal(true, a.alive);
146
+ done();
147
+ }).timeout(attemptTimeout);
148
+ }
149
+ });
150
+
151
+ /*
152
+ -----------------------------------------------------------------------
153
+ -----------------------------------------------------------------------
154
+ *** All code above this comment will be replaced during a migration ***
155
+ ******************* DO NOT REMOVE THIS COMMENT BLOCK ******************
156
+ -----------------------------------------------------------------------
157
+ -----------------------------------------------------------------------
158
+ */
159
+ const cloneOptions = {
160
+ dir: '../../test',
161
+ url: 'https://gitlab.com/test/test-project.git'
162
+ };
163
+ describe('#cloneRepo', () => {
164
+ if (!stub) {
165
+ it('should clone the repo', (done) => {
166
+ try {
167
+ a.cloneRepo(cloneOptions, (data, error) => {
168
+ try {
169
+ runCommonAsserts(data, error);
170
+ assert.equal(200, data.code);
171
+ done();
172
+ } catch (ex) {
173
+ log.error(`Test Failure: ${ex}`);
174
+ done(ex);
175
+ }
176
+ });
177
+ } catch (exc) {
178
+ log.error(`Adapter Exception: ${exc}`);
179
+ done(exc);
180
+ }
181
+ }).timeout(attemptTimeout);
182
+ }
183
+ });
184
+
185
+ const removeOptions = {
186
+ dir: '../../test',
187
+ filepath: 'README.md'
188
+ };
189
+ describe('#removeFile', () => {
190
+ if (!stub) {
191
+ it('should remove file', (done) => {
192
+ try {
193
+ a.removeFile(removeOptions, (data, error) => {
194
+ try {
195
+ runCommonAsserts(data, error);
196
+ assert.equal(200, data.code);
197
+ done();
198
+ } catch (ex) {
199
+ log.error(`Test Failure: ${ex}`);
200
+ done(ex);
201
+ }
202
+ });
203
+ } catch (exc) {
204
+ log.error(`Adapter Exception: ${exc}`);
205
+ done(exc);
206
+ }
207
+ }).timeout(attemptTimeout);
208
+ }
209
+ });
210
+
211
+ const options = {
212
+ dir: '../../test',
213
+ filepath: 'README.md'
214
+ };
215
+ describe('#getStatus', () => {
216
+ if (!stub) {
217
+ it('should get status of a file', (done) => {
218
+ try {
219
+ a.getStatus(options, (data, error) => {
220
+ try {
221
+ runCommonAsserts(data, error);
222
+ assert.equal(200, data.code);
223
+ done();
224
+ } catch (ex) {
225
+ log.error(`Test Failure: ${ex}`);
226
+ done(ex);
227
+ }
228
+ });
229
+ } catch (exc) {
230
+ log.error(`Adapter Exception: ${exc}`);
231
+ done(exc);
232
+ }
233
+ }).timeout(attemptTimeout);
234
+ }
235
+ });
236
+
237
+ const matrixOptions = {
238
+ dir: '../../test'
239
+ };
240
+ describe('#getStatusMatrix', () => {
241
+ if (!stub) {
242
+ it('should get status of multiple files', (done) => {
243
+ try {
244
+ a.getStatusMatrix(matrixOptions, (data, error) => {
245
+ try {
246
+ runCommonAsserts(data, error);
247
+ assert.equal(200, data.code);
248
+ done();
249
+ } catch (ex) {
250
+ log.error(`Test Failure: ${ex}`);
251
+ done(ex);
252
+ }
253
+ });
254
+ } catch (exc) {
255
+ log.error(`Adapter Exception: ${exc}`);
256
+ done(exc);
257
+ }
258
+ }).timeout(attemptTimeout);
259
+ }
260
+ });
261
+
262
+ const commitOptions = {
263
+ dir: '../../test',
264
+ message: 'delete README',
265
+ author: {
266
+ name: ''
267
+ }
268
+ };
269
+ describe('#createCommit', () => {
270
+ if (!stub) {
271
+ it('should create commit', (done) => {
272
+ try {
273
+ a.createCommit(commitOptions, (data, error) => {
274
+ try {
275
+ runCommonAsserts(data, error);
276
+ assert.equal(200, data.code);
277
+ done();
278
+ } catch (ex) {
279
+ log.error(`Test Failure: ${ex}`);
280
+ done(ex);
281
+ }
282
+ });
283
+ } catch (exc) {
284
+ log.error(`Adapter Exception: ${exc}`);
285
+ done(exc);
286
+ }
287
+ }).timeout(attemptTimeout);
288
+ }
289
+ });
290
+
291
+ const pushOptions = {
292
+ dir: '../../test',
293
+ remote: 'origin',
294
+ ref: 'main'
295
+ };
296
+ describe('#pushChanges', () => {
297
+ if (!stub) {
298
+ it('should push a branch', (done) => {
299
+ try {
300
+ a.pushChanges(pushOptions, (data, error) => {
301
+ try {
302
+ runCommonAsserts(data, error);
303
+ assert.equal(200, data.code);
304
+ done();
305
+ } catch (ex) {
306
+ log.error(`Test Failure: ${ex}`);
307
+ done(ex);
308
+ }
309
+ });
310
+ } catch (exc) {
311
+ log.error(`Adapter Exception: ${exc}`);
312
+ done(exc);
313
+ }
314
+ }).timeout(attemptTimeout);
315
+ }
316
+ });
317
+ });
318
+ });