@anmiles/google-api-wrapper 7.0.6 → 7.0.7
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.
- package/.eslintrc.js +1 -86
- package/.gitlab-ci.yml +0 -1
- package/.vscode/settings.json +0 -4
- package/CHANGELOG.md +7 -0
- package/README.md +5 -2
- package/dist/index.d.ts +5 -5
- package/dist/index.js +15 -15
- package/dist/lib/api/calendar.d.ts +3 -3
- package/dist/lib/api/calendar.js +13 -13
- package/dist/lib/api/shared.d.ts +23 -23
- package/dist/lib/api/shared.js +23 -23
- package/dist/lib/api/shared.js.map +1 -1
- package/dist/lib/api/youtube.d.ts +3 -3
- package/dist/lib/api/youtube.js +13 -13
- package/dist/lib/auth.d.ts +10 -10
- package/dist/lib/auth.js +34 -34
- package/dist/lib/auth.js.map +1 -1
- package/dist/lib/jsonLib.d.ts +14 -14
- package/dist/lib/jsonLib.js +54 -55
- package/dist/lib/jsonLib.js.map +1 -1
- package/dist/lib/paths.d.ts +16 -16
- package/dist/lib/paths.js +45 -45
- package/dist/lib/profiles.d.ts +10 -10
- package/dist/lib/profiles.js +32 -33
- package/dist/lib/profiles.js.map +1 -1
- package/dist/lib/secrets.d.ts +22 -22
- package/dist/lib/secrets.js +150 -146
- package/dist/lib/secrets.js.map +1 -1
- package/dist/lib/sleep.d.ts +6 -6
- package/dist/lib/sleep.js +10 -10
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.js +18 -18
- package/dist/types/secrets.d.ts +15 -15
- package/dist/types/secrets.js +2 -2
- package/package.json +16 -15
- package/src/lib/__tests__/auth.test.ts +23 -23
- package/src/lib/__tests__/jsonLib.test.ts +33 -41
- package/src/lib/__tests__/paths.test.ts +5 -5
- package/src/lib/__tests__/profiles.test.ts +10 -13
- package/src/lib/__tests__/secrets.test.ts +40 -48
- package/src/lib/api/__tests__/calendar.test.ts +3 -3
- package/src/lib/api/__tests__/shared.test.ts +10 -10
- package/src/lib/api/__tests__/youtube.test.ts +3 -3
- package/src/lib/api/shared.ts +1 -1
- package/src/lib/auth.ts +1 -1
- package/src/lib/jsonLib.ts +2 -2
- package/src/lib/profiles.ts +1 -2
- package/src/lib/secrets.ts +8 -4
- package/dist/lib/logger.d.ts +0 -12
- package/dist/lib/logger.js +0 -46
- package/dist/lib/logger.js.map +0 -1
- package/src/lib/__tests__/logger.test.ts +0 -57
- package/src/lib/logger.ts +0 -21
package/dist/lib/sleep.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sleep = void 0;
|
|
4
|
-
exports.default = { sleep };
|
|
5
|
-
async function sleep(milliSeconds) {
|
|
6
|
-
return new Promise((resolve) => {
|
|
7
|
-
setTimeout(resolve, milliSeconds);
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
exports.sleep = sleep;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sleep = void 0;
|
|
4
|
+
exports.default = { sleep };
|
|
5
|
+
async function sleep(milliSeconds) {
|
|
6
|
+
return new Promise((resolve) => {
|
|
7
|
+
setTimeout(resolve, milliSeconds);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
exports.sleep = sleep;
|
|
11
11
|
//# sourceMappingURL=sleep.js.map
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export interface CommonOptions {
|
|
2
|
-
hideProgress?: boolean;
|
|
3
|
-
}
|
|
1
|
+
export interface CommonOptions {
|
|
2
|
+
hideProgress?: boolean;
|
|
3
|
+
}
|
package/dist/types/common.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
//# sourceMappingURL=common.js.map
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './common';
|
|
2
|
-
export * from './secrets';
|
|
1
|
+
export * from './common';
|
|
2
|
+
export * from './secrets';
|
package/dist/types/index.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./common"), exports);
|
|
18
|
-
__exportStar(require("./secrets"), exports);
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./common"), exports);
|
|
18
|
+
__exportStar(require("./secrets"), exports);
|
|
19
19
|
//# sourceMappingURL=index.js.map
|
package/dist/types/secrets.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
export interface Secrets {
|
|
2
|
-
web: {
|
|
3
|
-
client_id: `${string}.apps.googleusercontent.com`;
|
|
4
|
-
project_id: string;
|
|
5
|
-
auth_uri: 'https://accounts.google.com/o/oauth2/auth';
|
|
6
|
-
token_uri: 'https://oauth2.googleapis.com/token';
|
|
7
|
-
auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs';
|
|
8
|
-
client_secret: string;
|
|
9
|
-
redirect_uris: string[];
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
export interface AuthOptions {
|
|
13
|
-
temporary?: boolean;
|
|
14
|
-
scopes?: string[];
|
|
15
|
-
}
|
|
1
|
+
export interface Secrets {
|
|
2
|
+
web: {
|
|
3
|
+
client_id: `${string}.apps.googleusercontent.com`;
|
|
4
|
+
project_id: string;
|
|
5
|
+
auth_uri: 'https://accounts.google.com/o/oauth2/auth';
|
|
6
|
+
token_uri: 'https://oauth2.googleapis.com/token';
|
|
7
|
+
auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs';
|
|
8
|
+
client_secret: string;
|
|
9
|
+
redirect_uris: string[];
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export interface AuthOptions {
|
|
13
|
+
temporary?: boolean;
|
|
14
|
+
scopes?: string[];
|
|
15
|
+
}
|
package/dist/types/secrets.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
//# sourceMappingURL=secrets.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anmiles/google-api-wrapper",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.7",
|
|
4
4
|
"description": "Provides quick interface for getting google API data",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"google",
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
"auth"
|
|
9
9
|
],
|
|
10
10
|
"author": "Anatoliy Oblaukhov",
|
|
11
|
-
"homepage": "https://gitlab.com/anmiles/google-api-wrapper",
|
|
12
|
-
"repository": "gitlab:anmiles/google-api-wrapper",
|
|
11
|
+
"homepage": "https://gitlab.com/anmiles/lib/google-api-wrapper",
|
|
12
|
+
"repository": "gitlab:anmiles/lib/google-api-wrapper",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=18.14.2"
|
|
@@ -29,25 +29,26 @@
|
|
|
29
29
|
"login": "node ./dist/login.js"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"
|
|
32
|
+
"@anmiles/logger": "^2.1.2",
|
|
33
33
|
"execa": "^5.1.1",
|
|
34
|
-
"googleapis": "^
|
|
34
|
+
"googleapis": "^118.0.0",
|
|
35
35
|
"open": "^8.4.2",
|
|
36
36
|
"server-destroy": "^1.0.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@
|
|
39
|
+
"@anmiles/eslint-config": "^1.0.6",
|
|
40
|
+
"@types/jest": "^29.5.1",
|
|
40
41
|
"@types/server-destroy": "^1.0.1",
|
|
41
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
42
|
-
"@typescript-eslint/parser": "^5.
|
|
43
|
-
"eslint": "^8.
|
|
42
|
+
"@typescript-eslint/eslint-plugin": "^5.59.2",
|
|
43
|
+
"@typescript-eslint/parser": "^5.59.2",
|
|
44
|
+
"eslint": "^8.40.0",
|
|
44
45
|
"eslint-plugin-align-assignments": "^1.1.2",
|
|
45
|
-
"eslint-plugin-import": "^2.
|
|
46
|
-
"eslint-plugin-jest": "^
|
|
47
|
-
"jest": "^
|
|
46
|
+
"eslint-plugin-import": "^2.27.5",
|
|
47
|
+
"eslint-plugin-jest": "^27.2.1",
|
|
48
|
+
"jest": "^29.5.0",
|
|
48
49
|
"nyc": "^15.1.0",
|
|
49
|
-
"rimraf": "^
|
|
50
|
-
"ts-jest": "^
|
|
51
|
-
"typescript": "^
|
|
50
|
+
"rimraf": "^5.0.0",
|
|
51
|
+
"ts-jest": "^29.1.0",
|
|
52
|
+
"typescript": "^5.0.4"
|
|
52
53
|
}
|
|
53
54
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { google } from 'googleapis';
|
|
2
2
|
import type GoogleApis from 'googleapis';
|
|
3
|
-
import logger from '
|
|
3
|
+
import logger from '@anmiles/logger';
|
|
4
4
|
import profiles from '../profiles';
|
|
5
5
|
import secrets from '../secrets';
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ jest.mock<typeof auth>('../auth', () => ({
|
|
|
11
11
|
getAuth : jest.fn().mockImplementation(async () => googleAuth),
|
|
12
12
|
}));
|
|
13
13
|
|
|
14
|
-
jest.mock<Partial<typeof logger>>('
|
|
14
|
+
jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
|
|
15
15
|
info : jest.fn(),
|
|
16
16
|
warn : jest.fn(),
|
|
17
17
|
}));
|
|
@@ -56,95 +56,95 @@ describe('src/lib/auth', () => {
|
|
|
56
56
|
describe('login', () => {
|
|
57
57
|
it('should get profiles', async () => {
|
|
58
58
|
await original.login();
|
|
59
|
-
expect(profiles.getProfiles).
|
|
59
|
+
expect(profiles.getProfiles).toHaveBeenCalledWith();
|
|
60
60
|
});
|
|
61
61
|
|
|
62
62
|
it('should auth all profiles', async () => {
|
|
63
63
|
await original.login();
|
|
64
64
|
|
|
65
65
|
allProfiles.forEach((profile) => {
|
|
66
|
-
expect(auth.getAuth).
|
|
66
|
+
expect(auth.getAuth).toHaveBeenCalledWith(profile, undefined);
|
|
67
67
|
});
|
|
68
68
|
});
|
|
69
69
|
|
|
70
70
|
it('should auth only specified profile', async () => {
|
|
71
71
|
await original.login('username1');
|
|
72
72
|
|
|
73
|
-
expect(auth.getAuth).
|
|
74
|
-
expect(auth.getAuth).not.
|
|
73
|
+
expect(auth.getAuth).toHaveBeenCalledWith('username1', undefined);
|
|
74
|
+
expect(auth.getAuth).not.toHaveBeenCalledWith('username2', undefined);
|
|
75
75
|
});
|
|
76
76
|
|
|
77
77
|
it('should pass temporariness for all profiles', async () => {
|
|
78
78
|
await original.login(undefined, { temporary : true });
|
|
79
79
|
|
|
80
|
-
expect(auth.getAuth).
|
|
81
|
-
expect(auth.getAuth).
|
|
80
|
+
expect(auth.getAuth).toHaveBeenCalledWith('username1', { temporary : true });
|
|
81
|
+
expect(auth.getAuth).toHaveBeenCalledWith('username2', { temporary : true });
|
|
82
82
|
|
|
83
83
|
});
|
|
84
84
|
|
|
85
85
|
it('should pass temporariness only for specified profile', async () => {
|
|
86
86
|
await original.login('username1', { temporary : true });
|
|
87
87
|
|
|
88
|
-
expect(auth.getAuth).
|
|
89
|
-
expect(auth.getAuth).not.
|
|
88
|
+
expect(auth.getAuth).toHaveBeenCalledWith('username1', { temporary : true });
|
|
89
|
+
expect(auth.getAuth).not.toHaveBeenCalledWith('username2', { temporary : true });
|
|
90
90
|
|
|
91
91
|
});
|
|
92
92
|
|
|
93
93
|
it('should show auth progress for all profiles by default', async () => {
|
|
94
94
|
await original.login();
|
|
95
95
|
|
|
96
|
-
expect(logger.warn).
|
|
97
|
-
expect(logger.warn).
|
|
98
|
-
expect(logger.info).
|
|
99
|
-
expect(logger.info).
|
|
96
|
+
expect(logger.warn).toHaveBeenCalledWith('username1 - logging in...');
|
|
97
|
+
expect(logger.warn).toHaveBeenCalledWith('username2 - logging in...');
|
|
98
|
+
expect(logger.info).toHaveBeenCalledWith('username1 - logged in successfully');
|
|
99
|
+
expect(logger.info).toHaveBeenCalledWith('username2 - logged in successfully');
|
|
100
100
|
});
|
|
101
101
|
|
|
102
102
|
it('should show auth progress for specified profile by default', async () => {
|
|
103
103
|
await original.login('username1');
|
|
104
104
|
|
|
105
|
-
expect(logger.warn).
|
|
106
|
-
expect(logger.info).
|
|
105
|
+
expect(logger.warn).toHaveBeenCalledWith('username1 - logging in...');
|
|
106
|
+
expect(logger.info).toHaveBeenCalledWith('username1 - logged in successfully');
|
|
107
107
|
});
|
|
108
108
|
|
|
109
109
|
it('should not show auth progress if hidden', async () => {
|
|
110
110
|
await original.login(undefined, { hideProgress : true });
|
|
111
111
|
await original.login('username1', { hideProgress : true });
|
|
112
112
|
|
|
113
|
-
expect(logger.info).not.
|
|
113
|
+
expect(logger.info).not.toHaveBeenCalled();
|
|
114
114
|
});
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
describe('getAuth', () => {
|
|
118
118
|
it('should get secrets', async () => {
|
|
119
119
|
await original.getAuth(profile);
|
|
120
|
-
expect(secrets.getSecrets).
|
|
120
|
+
expect(secrets.getSecrets).toHaveBeenCalledWith(profile);
|
|
121
121
|
});
|
|
122
122
|
|
|
123
123
|
it('should get credentials', async () => {
|
|
124
124
|
await original.getAuth(profile);
|
|
125
|
-
expect(secrets.getCredentials).
|
|
125
|
+
expect(secrets.getCredentials).toHaveBeenCalledWith(profile, googleAuth, undefined);
|
|
126
126
|
});
|
|
127
127
|
|
|
128
128
|
it('should create OAuth2 instance', async () => {
|
|
129
129
|
await original.getAuth(profile);
|
|
130
|
-
expect(google.auth.OAuth2).
|
|
130
|
+
expect(google.auth.OAuth2).toHaveBeenCalledWith(secretsObject.web.client_id, secretsObject.web.client_secret, secretsObject.web.redirect_uris[0]);
|
|
131
131
|
});
|
|
132
132
|
|
|
133
133
|
it('should set credentials', async () => {
|
|
134
134
|
await original.getAuth(profile);
|
|
135
|
-
expect(googleAuth.setCredentials).
|
|
135
|
+
expect(googleAuth.setCredentials).toHaveBeenCalledWith(credentials);
|
|
136
136
|
});
|
|
137
137
|
|
|
138
138
|
it('should pass temporariness', async () => {
|
|
139
139
|
await original.getAuth(profile, { temporary : true });
|
|
140
140
|
|
|
141
|
-
expect(secrets.getCredentials).
|
|
141
|
+
expect(secrets.getCredentials).toHaveBeenCalledWith(profile, googleAuth, { temporary : true });
|
|
142
142
|
});
|
|
143
143
|
|
|
144
144
|
it('should set google auth', async () => {
|
|
145
145
|
await original.getAuth(profile);
|
|
146
146
|
|
|
147
|
-
expect(google.options).
|
|
147
|
+
expect(google.options).toHaveBeenCalledWith({ auth : googleAuth });
|
|
148
148
|
});
|
|
149
149
|
});
|
|
150
150
|
});
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
-
import logger from '../logger';
|
|
3
2
|
import paths from '../paths';
|
|
4
3
|
|
|
5
4
|
import jsonLib from '../jsonLib';
|
|
@@ -18,12 +17,6 @@ jest.mock<Partial<typeof fs>>('fs', () => ({
|
|
|
18
17
|
existsSync : jest.fn().mockImplementation(() => fileExists),
|
|
19
18
|
}));
|
|
20
19
|
|
|
21
|
-
jest.mock<Partial<typeof logger>>('../logger', () => ({
|
|
22
|
-
error : jest.fn().mockImplementation((error) => {
|
|
23
|
-
throw error;
|
|
24
|
-
}) as jest.Mock<never, any>,
|
|
25
|
-
}));
|
|
26
|
-
|
|
27
20
|
jest.mock<Partial<typeof paths>>('../paths', () => ({
|
|
28
21
|
ensureFile : jest.fn(),
|
|
29
22
|
}));
|
|
@@ -47,7 +40,7 @@ describe('src/lib/jsonLib', () => {
|
|
|
47
40
|
it('should read specified file', () => {
|
|
48
41
|
original.readJSON(filename);
|
|
49
42
|
|
|
50
|
-
expect(fs.readFileSync).
|
|
43
|
+
expect(fs.readFileSync).toHaveBeenCalledWith(filename);
|
|
51
44
|
});
|
|
52
45
|
|
|
53
46
|
it('should return parsed JSON', () => {
|
|
@@ -61,7 +54,7 @@ describe('src/lib/jsonLib', () => {
|
|
|
61
54
|
it('should write JSON into specified file', () => {
|
|
62
55
|
original.writeJSON(filename, json);
|
|
63
56
|
|
|
64
|
-
expect(fs.writeFileSync).
|
|
57
|
+
expect(fs.writeFileSync).toHaveBeenCalledWith(filename, jsonString);
|
|
65
58
|
});
|
|
66
59
|
});
|
|
67
60
|
|
|
@@ -73,8 +66,8 @@ describe('src/lib/jsonLib', () => {
|
|
|
73
66
|
|
|
74
67
|
original.getJSON(filename, createCallback, validateCallback);
|
|
75
68
|
|
|
76
|
-
expect(jsonLib.readJSON).
|
|
77
|
-
expect(createCallback).not.
|
|
69
|
+
expect(jsonLib.readJSON).toHaveBeenCalledWith(filename);
|
|
70
|
+
expect(createCallback).not.toHaveBeenCalled();
|
|
78
71
|
});
|
|
79
72
|
|
|
80
73
|
it('should call createCallback if file exists but json is not valid', () => {
|
|
@@ -83,8 +76,8 @@ describe('src/lib/jsonLib', () => {
|
|
|
83
76
|
|
|
84
77
|
original.getJSON(filename, createCallback, validateCallback);
|
|
85
78
|
|
|
86
|
-
expect(jsonLib.readJSON).
|
|
87
|
-
expect(createCallback).
|
|
79
|
+
expect(jsonLib.readJSON).toHaveBeenCalledWith(filename);
|
|
80
|
+
expect(createCallback).toHaveBeenCalledWith();
|
|
88
81
|
});
|
|
89
82
|
|
|
90
83
|
it('should call createCallback if file not exists', () => {
|
|
@@ -92,8 +85,8 @@ describe('src/lib/jsonLib', () => {
|
|
|
92
85
|
|
|
93
86
|
original.getJSON(filename, createCallback, validateCallback);
|
|
94
87
|
|
|
95
|
-
expect(jsonLib.readJSON).not.
|
|
96
|
-
expect(createCallback).
|
|
88
|
+
expect(jsonLib.readJSON).not.toHaveBeenCalled();
|
|
89
|
+
expect(createCallback).toHaveBeenCalledWith();
|
|
97
90
|
});
|
|
98
91
|
|
|
99
92
|
it('should not write fallback JSON back if file exists and json is valid', () => {
|
|
@@ -102,7 +95,7 @@ describe('src/lib/jsonLib', () => {
|
|
|
102
95
|
|
|
103
96
|
original.getJSON(filename, createCallback, validateCallback);
|
|
104
97
|
|
|
105
|
-
expect(jsonLib.writeJSON).not.
|
|
98
|
+
expect(jsonLib.writeJSON).not.toHaveBeenCalled();
|
|
106
99
|
});
|
|
107
100
|
|
|
108
101
|
it('should write fallback JSON back if file exists but json is not valid', () => {
|
|
@@ -111,9 +104,9 @@ describe('src/lib/jsonLib', () => {
|
|
|
111
104
|
|
|
112
105
|
original.getJSON(filename, createCallback, validateCallback);
|
|
113
106
|
|
|
114
|
-
expect(jsonLib.checkJSON).
|
|
115
|
-
expect(paths.ensureFile).
|
|
116
|
-
expect(jsonLib.writeJSON).
|
|
107
|
+
expect(jsonLib.checkJSON).toHaveBeenCalledWith(filename, fallbackJSON);
|
|
108
|
+
expect(paths.ensureFile).toHaveBeenCalledWith(filename);
|
|
109
|
+
expect(jsonLib.writeJSON).toHaveBeenCalledWith(filename, fallbackJSON);
|
|
117
110
|
});
|
|
118
111
|
|
|
119
112
|
it('should write fallback JSON back if file not exists', () => {
|
|
@@ -121,9 +114,9 @@ describe('src/lib/jsonLib', () => {
|
|
|
121
114
|
|
|
122
115
|
original.getJSON(filename, createCallback, validateCallback);
|
|
123
116
|
|
|
124
|
-
expect(jsonLib.checkJSON).
|
|
125
|
-
expect(paths.ensureFile).
|
|
126
|
-
expect(jsonLib.writeJSON).
|
|
117
|
+
expect(jsonLib.checkJSON).toHaveBeenCalledWith(filename, fallbackJSON);
|
|
118
|
+
expect(paths.ensureFile).toHaveBeenCalledWith(filename);
|
|
119
|
+
expect(jsonLib.writeJSON).toHaveBeenCalledWith(filename, fallbackJSON);
|
|
127
120
|
});
|
|
128
121
|
|
|
129
122
|
it('should return JSON if file exists and json is valid', () => {
|
|
@@ -160,8 +153,8 @@ describe('src/lib/jsonLib', () => {
|
|
|
160
153
|
|
|
161
154
|
await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
|
|
162
155
|
|
|
163
|
-
expect(jsonLib.readJSON).
|
|
164
|
-
expect(createCallbackAsync).not.
|
|
156
|
+
expect(jsonLib.readJSON).toHaveBeenCalledWith(filename);
|
|
157
|
+
expect(createCallbackAsync).not.toHaveBeenCalled();
|
|
165
158
|
});
|
|
166
159
|
|
|
167
160
|
it('should call createCallback if file exists but json is not valid', async () => {
|
|
@@ -170,8 +163,8 @@ describe('src/lib/jsonLib', () => {
|
|
|
170
163
|
|
|
171
164
|
await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
|
|
172
165
|
|
|
173
|
-
expect(jsonLib.readJSON).
|
|
174
|
-
expect(createCallbackAsync).
|
|
166
|
+
expect(jsonLib.readJSON).toHaveBeenCalledWith(filename);
|
|
167
|
+
expect(createCallbackAsync).toHaveBeenCalledWith();
|
|
175
168
|
});
|
|
176
169
|
|
|
177
170
|
it('should call createCallback if file not exists', async () => {
|
|
@@ -179,8 +172,8 @@ describe('src/lib/jsonLib', () => {
|
|
|
179
172
|
|
|
180
173
|
await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
|
|
181
174
|
|
|
182
|
-
expect(jsonLib.readJSON).not.
|
|
183
|
-
expect(createCallbackAsync).
|
|
175
|
+
expect(jsonLib.readJSON).not.toHaveBeenCalled();
|
|
176
|
+
expect(createCallbackAsync).toHaveBeenCalledWith();
|
|
184
177
|
});
|
|
185
178
|
|
|
186
179
|
it('should not write fallback JSON back if file exists and json is valid', async () => {
|
|
@@ -189,7 +182,7 @@ describe('src/lib/jsonLib', () => {
|
|
|
189
182
|
|
|
190
183
|
await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
|
|
191
184
|
|
|
192
|
-
expect(jsonLib.writeJSON).not.
|
|
185
|
+
expect(jsonLib.writeJSON).not.toHaveBeenCalled();
|
|
193
186
|
});
|
|
194
187
|
|
|
195
188
|
it('should write fallback JSON back if file exists but json is not valid', async () => {
|
|
@@ -198,9 +191,9 @@ describe('src/lib/jsonLib', () => {
|
|
|
198
191
|
|
|
199
192
|
await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
|
|
200
193
|
|
|
201
|
-
expect(jsonLib.checkJSON).
|
|
202
|
-
expect(paths.ensureFile).
|
|
203
|
-
expect(jsonLib.writeJSON).
|
|
194
|
+
expect(jsonLib.checkJSON).toHaveBeenCalledWith(filename, fallbackJSON);
|
|
195
|
+
expect(paths.ensureFile).toHaveBeenCalledWith(filename);
|
|
196
|
+
expect(jsonLib.writeJSON).toHaveBeenCalledWith(filename, fallbackJSON);
|
|
204
197
|
});
|
|
205
198
|
|
|
206
199
|
it('should write fallback JSON back if file not exists', async () => {
|
|
@@ -208,9 +201,9 @@ describe('src/lib/jsonLib', () => {
|
|
|
208
201
|
|
|
209
202
|
await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
|
|
210
203
|
|
|
211
|
-
expect(jsonLib.checkJSON).
|
|
212
|
-
expect(paths.ensureFile).
|
|
213
|
-
expect(jsonLib.writeJSON).
|
|
204
|
+
expect(jsonLib.checkJSON).toHaveBeenCalledWith(filename, fallbackJSON);
|
|
205
|
+
expect(paths.ensureFile).toHaveBeenCalledWith(filename);
|
|
206
|
+
expect(jsonLib.writeJSON).toHaveBeenCalledWith(filename, fallbackJSON);
|
|
214
207
|
});
|
|
215
208
|
|
|
216
209
|
it('should return JSON if file exists and json is valid', async () => {
|
|
@@ -241,13 +234,12 @@ describe('src/lib/jsonLib', () => {
|
|
|
241
234
|
});
|
|
242
235
|
|
|
243
236
|
describe('checkJSON', () => {
|
|
244
|
-
it('should do nothing if json is truthy', () => {
|
|
245
|
-
original.checkJSON(filename, json);
|
|
246
|
-
|
|
247
|
-
expect(logger.error).not.toBeCalled();
|
|
248
|
-
});
|
|
249
237
|
it('should output error if json is falsy', () => {
|
|
250
|
-
expect(() => original.checkJSON(filename, '')).
|
|
238
|
+
expect(() => original.checkJSON(filename, '')).toThrow(`File ${filename} doesn't exist and should be created with initial data, but function createCallback returned nothing`);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('should do nothing if json is truthy', () => {
|
|
242
|
+
expect(() => original.checkJSON(filename, json)).not.toThrow();
|
|
251
243
|
});
|
|
252
244
|
});
|
|
253
245
|
});
|
|
@@ -42,7 +42,7 @@ describe('src/lib/paths', () => {
|
|
|
42
42
|
|
|
43
43
|
original.ensureDir(dirPath);
|
|
44
44
|
|
|
45
|
-
expect(fs.mkdirSync).
|
|
45
|
+
expect(fs.mkdirSync).toHaveBeenCalledWith(dirPath, { recursive : true });
|
|
46
46
|
});
|
|
47
47
|
|
|
48
48
|
it('should not create empty dir if already exists', () => {
|
|
@@ -50,7 +50,7 @@ describe('src/lib/paths', () => {
|
|
|
50
50
|
|
|
51
51
|
original.ensureDir(dirPath);
|
|
52
52
|
|
|
53
|
-
expect(fs.writeFileSync).not.
|
|
53
|
+
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
|
54
54
|
});
|
|
55
55
|
|
|
56
56
|
it('should return dirPath', () => {
|
|
@@ -66,7 +66,7 @@ describe('src/lib/paths', () => {
|
|
|
66
66
|
|
|
67
67
|
original.ensureFile(filePath);
|
|
68
68
|
|
|
69
|
-
expect(paths.ensureDir).
|
|
69
|
+
expect(paths.ensureDir).toHaveBeenCalledWith('parentDir');
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
it('should create empty file if not exists', () => {
|
|
@@ -74,7 +74,7 @@ describe('src/lib/paths', () => {
|
|
|
74
74
|
|
|
75
75
|
original.ensureFile(filePath);
|
|
76
76
|
|
|
77
|
-
expect(fs.writeFileSync).
|
|
77
|
+
expect(fs.writeFileSync).toHaveBeenCalledWith(filePath, '');
|
|
78
78
|
});
|
|
79
79
|
|
|
80
80
|
it('should not create empty file if already exists', () => {
|
|
@@ -82,7 +82,7 @@ describe('src/lib/paths', () => {
|
|
|
82
82
|
|
|
83
83
|
original.ensureFile(filePath);
|
|
84
84
|
|
|
85
|
-
expect(fs.writeFileSync).not.
|
|
85
|
+
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
|
86
86
|
});
|
|
87
87
|
|
|
88
88
|
it('should return filePath', () => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
+
import logger from '@anmiles/logger';
|
|
2
3
|
import jsonLib from '../jsonLib';
|
|
3
|
-
import logger from '../logger';
|
|
4
4
|
import paths from '../paths';
|
|
5
5
|
|
|
6
6
|
import profiles from '../profiles';
|
|
@@ -23,12 +23,9 @@ jest.mock<Partial<typeof jsonLib>>('../jsonLib', () => ({
|
|
|
23
23
|
writeJSON : jest.fn(),
|
|
24
24
|
}));
|
|
25
25
|
|
|
26
|
-
jest.mock<Partial<typeof logger>>('
|
|
27
|
-
log
|
|
28
|
-
warn
|
|
29
|
-
error : jest.fn().mockImplementation((error) => {
|
|
30
|
-
throw error;
|
|
31
|
-
}) as jest.Mock<never, any>,
|
|
26
|
+
jest.mock<Partial<typeof logger>>('@anmiles/logger', () => ({
|
|
27
|
+
log : jest.fn(),
|
|
28
|
+
warn : jest.fn(),
|
|
32
29
|
}));
|
|
33
30
|
|
|
34
31
|
jest.mock<Partial<typeof paths>>('../paths', () => ({
|
|
@@ -56,7 +53,7 @@ describe('src/lib/profiles', () => {
|
|
|
56
53
|
it('should get json from profiles file', () => {
|
|
57
54
|
original.getProfiles();
|
|
58
55
|
|
|
59
|
-
expect(getJSONSpy).
|
|
56
|
+
expect(getJSONSpy).toHaveBeenCalled();
|
|
60
57
|
expect(getJSONSpy.mock.calls[0][0]).toEqual(profilesFile);
|
|
61
58
|
});
|
|
62
59
|
|
|
@@ -79,7 +76,7 @@ describe('src/lib/profiles', () => {
|
|
|
79
76
|
it('should write json to profiles file', () => {
|
|
80
77
|
original.setProfiles(allProfiles);
|
|
81
78
|
|
|
82
|
-
expect(jsonLib.writeJSON).
|
|
79
|
+
expect(jsonLib.writeJSON).toHaveBeenCalledWith(profilesFile, allProfiles);
|
|
83
80
|
});
|
|
84
81
|
});
|
|
85
82
|
|
|
@@ -87,7 +84,7 @@ describe('src/lib/profiles', () => {
|
|
|
87
84
|
it('should output error and do nothing if profile is falsy', () => {
|
|
88
85
|
const func = () => original.createProfile('');
|
|
89
86
|
|
|
90
|
-
expect(func).
|
|
87
|
+
expect(func).toThrow('Usage: `npm run create <profile>` where `profile` - is any profile name you want');
|
|
91
88
|
});
|
|
92
89
|
|
|
93
90
|
it('should get profiles', () => {
|
|
@@ -95,7 +92,7 @@ describe('src/lib/profiles', () => {
|
|
|
95
92
|
|
|
96
93
|
original.createProfile(newProfile);
|
|
97
94
|
|
|
98
|
-
expect(profiles.getProfiles).
|
|
95
|
+
expect(profiles.getProfiles).toHaveBeenCalledWith();
|
|
99
96
|
});
|
|
100
97
|
|
|
101
98
|
it('should not save profiles if profile already exists', () => {
|
|
@@ -103,7 +100,7 @@ describe('src/lib/profiles', () => {
|
|
|
103
100
|
|
|
104
101
|
original.createProfile(newProfile);
|
|
105
102
|
|
|
106
|
-
expect(profiles.setProfiles).not.
|
|
103
|
+
expect(profiles.setProfiles).not.toHaveBeenCalled();
|
|
107
104
|
});
|
|
108
105
|
|
|
109
106
|
it('should add new profile if not exists', () => {
|
|
@@ -111,7 +108,7 @@ describe('src/lib/profiles', () => {
|
|
|
111
108
|
|
|
112
109
|
original.createProfile(newProfile);
|
|
113
110
|
|
|
114
|
-
expect(profiles.setProfiles).
|
|
111
|
+
expect(profiles.setProfiles).toHaveBeenCalledWith([ 'username1', 'username2', 'newProfile' ]);
|
|
115
112
|
});
|
|
116
113
|
});
|
|
117
114
|
});
|