Skip to content

Commit 530f1c3

Browse files
committed
chore: unit tests
1 parent 79df7dd commit 530f1c3

File tree

8 files changed

+100
-39
lines changed

8 files changed

+100
-39
lines changed

lib/common/logger/logger.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import {
1414
import { IDictionary } from "../declarations";
1515
import { injector } from "../yok";
1616
import { color } from "../../color";
17-
const TerminalRenderer = require("marked-terminal");
17+
import { markedTerminal } from "marked-terminal";
1818

1919
export class Logger implements ILogger {
2020
private log4jsLogger: log4js.Logger = null;
21-
private passwordRegex = /(password=).*?(['&,]|$)|(password["']?\s*:\s*["']).*?(["'])/i;
21+
private passwordRegex =
22+
/(password=).*?(['&,]|$)|(password["']?\s*:\s*["']).*?(["'])/i;
2223
private passwordReplacement = "$1$3*******$2$4";
2324
private defaultLogLevel: LoggerLevel;
2425

@@ -61,7 +62,7 @@ export class Logger implements ILogger {
6162
if (level === LoggerLevel.TRACE || level === LoggerLevel.ALL) {
6263
this.warn(
6364
`The "${level}" log level might print some sensitive data like secrets or access tokens in request URLs. Be careful when you share this output.`,
64-
{ wrapMessageWithBorders: true }
65+
{ wrapMessageWithBorders: true },
6566
);
6667
}
6768
}
@@ -149,9 +150,9 @@ export class Logger implements ILogger {
149150
},
150151
};
151152

152-
marked.setOptions({ renderer: new TerminalRenderer(opts) });
153+
marked.use(markedTerminal(opts) as any);
153154

154-
const formattedMessage = marked(util.format.apply(null, args));
155+
const formattedMessage = marked.parse(util.format.apply(null, args));
155156
this.info(formattedMessage, { [LoggerConfigData.skipNewLine]: true });
156157
}
157158

@@ -180,17 +181,18 @@ export class Logger implements ILogger {
180181

181182
(<IDictionary<any>>this.log4jsLogger)[logMethod.toLowerCase()].apply(
182183
this.log4jsLogger,
183-
data
184+
data,
184185
);
185186

186187
for (const prop in logOpts) {
187188
this.log4jsLogger.removeContext(prop);
188189
}
189190
}
190191

191-
private getLogOptionsForMessage(
192-
data: any[]
193-
): { data: any[]; [key: string]: any } {
192+
private getLogOptionsForMessage(data: any[]): {
193+
data: any[];
194+
[key: string]: any;
195+
} {
194196
const loggerOptionKeys = _.keys(LoggerConfigData);
195197
const dataToCheck = data.filter((el) => {
196198
// objects created with Object.create(null) do not have `hasOwnProperty` function
@@ -238,7 +240,7 @@ export class Logger implements ILogger {
238240
if (typeof argument === "string" && !!argument.match(/password/i)) {
239241
argument = argument.replace(
240242
this.passwordRegex,
241-
this.passwordReplacement
243+
this.passwordReplacement,
242244
);
243245
}
244246

lib/common/test/unit-tests/errors.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -83,38 +83,38 @@ describe("errors", () => {
8383
const assertProcessExited = () => {
8484
assert.isTrue(
8585
isProcessExitCalled,
86-
"When the action fails, process.exit must be called."
86+
"When the action fails, process.exit must be called.",
8787
);
8888
assert.equal(
8989
processExitCode,
9090
127,
91-
"When the action fails, process.exit must be called with 127 by default."
91+
"When the action fails, process.exit must be called with 127 by default.",
9292
);
9393
};
9494

9595
const assertPrintCommandHelpSuggestionIsNotCalled = () => {
9696
assert.isFalse(
9797
isPrintCommandHelpSuggestionExecuted,
98-
"printCommandHelpSuggestion should not be called when the suggestCommandHelp is not set to the exception."
98+
"printCommandHelpSuggestion should not be called when the suggestCommandHelp is not set to the exception.",
9999
);
100100
};
101101

102102
it("executes the passed action and does not print anything when it is successful", async () => {
103103
const { action, printCommandHelpSuggestion } = setupTest();
104104
let result = await errors.beginCommand(
105105
action,
106-
printCommandHelpSuggestion
106+
printCommandHelpSuggestion,
107107
);
108108
assert.isTrue(
109109
result,
110-
"beginCommand must return the result of the passed action."
110+
"beginCommand must return the result of the passed action.",
111111
);
112112

113113
actionResult = false;
114114
result = await errors.beginCommand(action, printCommandHelpSuggestion);
115115
assert.isFalse(
116116
result,
117-
"beginCommand must return the result of the passed action."
117+
"beginCommand must return the result of the passed action.",
118118
);
119119
assert.equal(logger.errorOutput, "");
120120
assert.equal(logger.output, "");
@@ -136,15 +136,15 @@ describe("errors", () => {
136136
const assertCallStack = () => {
137137
assert.isTrue(
138138
logger.errorOutput.indexOf(errMsg) !== -1,
139-
"The error output must contain the error message"
139+
"The error output must contain the error message",
140140
);
141141
assert.isTrue(
142-
logger.errorOutput.indexOf("at Generator.next") !== -1,
143-
"The error output must contain callstack"
142+
logger.errorOutput.indexOf("at next") !== -1,
143+
"The error output must contain callstack",
144144
);
145145
assert.isTrue(
146146
logger.errorOutput.indexOf(path.join("lib", "common")) !== -1,
147-
"The error output must contain path to lib/common, as this is the location of the file"
147+
"The error output must contain path to lib/common, as this is the location of the file",
148148
);
149149
};
150150

@@ -213,7 +213,7 @@ describe("errors", () => {
213213
assertProcessExited();
214214
assert.isTrue(
215215
isPrintCommandHelpSuggestionExecuted,
216-
"printCommandHelpSuggestion should be called when the action fails with an error object for which suggestCommandHelp is true."
216+
"printCommandHelpSuggestion should be called when the action fails with an error object for which suggestCommandHelp is true.",
217217
);
218218
});
219219

@@ -225,12 +225,12 @@ describe("errors", () => {
225225
assert.equal(logger.errorOutput, `${errMsg}\n`);
226226
assert.isTrue(
227227
isProcessExitCalled,
228-
"When the action fails, process.exit must be called."
228+
"When the action fails, process.exit must be called.",
229229
);
230230
assert.equal(
231231
processExitCode,
232232
errObj.errorCode,
233-
`When the action fails, process.exit must be called with ${errObj.errorCode}.`
233+
`When the action fails, process.exit must be called with ${errObj.errorCode}.`,
234234
);
235235
});
236236

@@ -242,12 +242,12 @@ describe("errors", () => {
242242
assert.equal(logger.errorOutput, `${errMsg}\n`);
243243
assert.isTrue(
244244
isProcessExitCalled,
245-
"When the action fails, process.exit must be called."
245+
"When the action fails, process.exit must be called.",
246246
);
247247
assert.equal(
248248
processExitCode,
249249
127,
250-
"When the action fails, process.exit must be called with 127 by default."
250+
"When the action fails, process.exit must be called with 127 by default.",
251251
);
252252
});
253253
});

lib/common/test/unit-tests/file-system.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "path";
33
import * as temp from "temp";
44
import * as hostInfoLib from "../../host-info";
55
import { assert, use } from "chai";
6+
import "chai-as-promised";
67
import chaiAsPromised from "chai-as-promised";
78
import * as fileSystemFile from "../../file-system";
89
import * as childProcessLib from "../../child-process";

lib/common/test/unit-tests/mobile/devices-service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { EventEmitter } from "events";
1515
import { assert, use } from "chai";
1616
import * as util from "util";
1717
import * as _ from "lodash";
18+
import "chai-as-promised";
1819
import chaiAsPromised from "chai-as-promised";
1920

2021
use(chaiAsPromised);

package-lock.json

+47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nativescript",
33
"main": "./lib/nativescript-cli-lib.js",
4-
"version": "8.9.0",
4+
"version": "8.9.0-dev.0",
55
"author": "NativeScript <[email protected]>",
66
"description": "Command-line interface for building NativeScript projects",
77
"bin": {
@@ -123,6 +123,7 @@
123123
"@types/convert-source-map": "2.0.3",
124124
"@types/glob": "^8.1.0",
125125
"@types/lodash": "4.17.15",
126+
"@types/marked-terminal": "^6.1.1",
126127
"@types/node": "^20.0.0",
127128
"@types/npmcli__arborist": "^6.3.0",
128129
"@types/pacote": "^11.1.8",

0 commit comments

Comments
 (0)