Skip to content

Commit 5478891

Browse files
fix: save and remove of user-settings are not working
Due to recent changes, trying to save a setting in the user-settings.json or to remove it from there is not working. This breaks all new users and the execution of commands like `tns usage-reporting enable/disable`, `tns error-reporting enable/disable`. The problem is an infinite recursion in the userSettingsService, which should just call jsonFileSettingsService, but instead it calls itself. Fix the recursion and add unit tests for the userSettingsService to ensure it acts as a proxy to jsonFileSettingsService.
1 parent b1e57b3 commit 5478891

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

lib/services/user-settings-service.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export class UserSettingsService implements IUserSettingsService {
55
const userSettingsFilePath = path.join(this.$settingsService.getProfileDir(), "user-settings.json");
66
return this.$injector.resolve("jsonFileSettingsService", { jsonFileSettingsPath: userSettingsFilePath });
77
}
8+
89
constructor(private $injector: IInjector,
910
private $settingsService: ISettingsService) {
1011
}
@@ -14,15 +15,15 @@ export class UserSettingsService implements IUserSettingsService {
1415
}
1516

1617
public saveSetting<T>(key: string, value: T, cacheOpts?: IUseCacheOpts): Promise<void> {
17-
return this.saveSetting<T>(key, value, cacheOpts);
18+
return this.$jsonFileSettingsService.saveSetting<T>(key, value, cacheOpts);
1819
}
1920

2021
public saveSettings(data: IDictionary<{}>, cacheOpts?: IUseCacheOpts): Promise<void> {
2122
return this.$jsonFileSettingsService.saveSettings(data, cacheOpts);
2223
}
2324

2425
public removeSetting(key: string): Promise<void> {
25-
return this.removeSetting(key);
26+
return this.$jsonFileSettingsService.removeSetting(key);
2627
}
2728

2829
public loadUserSettingsFile(): Promise<void> {
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Yok } from "../../lib/common/yok";
2+
import { UserSettingsService } from "../../lib/services/user-settings-service";
3+
import { assert } from "chai";
4+
import * as path from "path";
5+
6+
class JsonFileSettingsServiceMock {
7+
constructor(public jsonFileSettingsPath: string) { }
8+
}
9+
10+
describe("userSettingsService", () => {
11+
const profileDir = "my-profile-dir";
12+
const expectedJsonFileSettingsFilePath = path.join(profileDir, "user-settings.json");
13+
14+
const createTestInjector = (): IInjector => {
15+
const testInjector = new Yok();
16+
testInjector.register("settingsService", {
17+
getProfileDir: () => profileDir
18+
});
19+
20+
testInjector.register("jsonFileSettingsService", JsonFileSettingsServiceMock);
21+
testInjector.register("userSettingsService", UserSettingsService);
22+
return testInjector;
23+
};
24+
25+
const testCases = [
26+
{
27+
methodName: "getSettingValue",
28+
input: ["settingName"],
29+
expectedArgs: [
30+
"settingName",
31+
undefined
32+
]
33+
},
34+
{
35+
methodName: "saveSetting",
36+
input: ["settingName", "settingValue"],
37+
expectedArgs: [
38+
"settingName",
39+
"settingValue",
40+
undefined
41+
]
42+
},
43+
{
44+
methodName: "saveSettings",
45+
input: [{ value: { subValue: 1 } }],
46+
expectedArgs: [
47+
{ value: { subValue: 1 } },
48+
undefined
49+
]
50+
},
51+
{
52+
methodName: "removeSetting",
53+
input: ["settingName"],
54+
expectedArgs: [
55+
"settingName"
56+
]
57+
},
58+
{
59+
methodName: "loadUserSettingsFile",
60+
input: [],
61+
expectedArgs: []
62+
}
63+
];
64+
65+
for (const testCase of testCases) {
66+
it(`calls ${testCase.methodName} method of jsonFileSettingsService with correct args`, async () => {
67+
const testInjector = createTestInjector();
68+
const dataPassedToJsonFileSettingsService: any[] = [];
69+
const userSettingsService = testInjector.resolve("userSettingsService");
70+
const jsonFileSettingsService = userSettingsService.$jsonFileSettingsService;
71+
72+
jsonFileSettingsService[testCase.methodName] = async (...args: any[]): Promise<void> => {
73+
dataPassedToJsonFileSettingsService.push(...args);
74+
};
75+
76+
await userSettingsService[testCase.methodName](...testCase.input);
77+
78+
assert.deepEqual(dataPassedToJsonFileSettingsService, testCase.expectedArgs);
79+
assert.equal(jsonFileSettingsService.jsonFileSettingsPath, expectedJsonFileSettingsFilePath);
80+
});
81+
}
82+
});

0 commit comments

Comments
 (0)