Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit ba288cb

Browse files
committed
Mock more fs
1 parent d9c7aec commit ba288cb

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/plugins/login/utils/simpleFileTokenCache.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@ import fs from "fs";
22
import mockFs from "mock-fs";
33
import { MockFactory } from "../../../test/mockFactory";
44
import { SimpleFileTokenCache } from "./simpleFileTokenCache";
5-
import os from "os";
65

76
describe("Simple File Token Cache", () => {
87
const tokenFilePath = "slsTokenCache.json";
8+
const mockReadDirSync = jest.fn();
9+
const mockWriteFileSync = jest.fn(() => {
10+
return
11+
});
12+
const mockMkDir = jest.fn((path) => {
13+
return
14+
});
915

1016
let fileContent = {
1117
entries: [],
1218
subscriptions: []
1319
};
1420

21+
beforeEach(() => {
22+
fs.readdirSync = mockReadDirSync;
23+
fs.mkdirSync = mockMkDir;
24+
fs.writeFileSync = mockWriteFileSync;
25+
})
26+
1527
afterEach(() => {
28+
mockReadDirSync.mockRestore();
29+
mockMkDir.mockRestore();
30+
mockWriteFileSync.mockRestore();
1631
mockFs.restore();
1732
});
1833

@@ -36,19 +51,9 @@ describe("Simple File Token Cache", () => {
3651

3752
it("Create .azure default directory if it doesn't exist", () => {
3853
mockFs();
39-
const mockMkDir = jest.fn((path) => {
40-
return
41-
});
42-
const mockHomedir = jest.fn(() => {
43-
return ""
44-
});
45-
fs.mkdirSync = mockMkDir;
46-
os.homedir = mockHomedir;
4754
new SimpleFileTokenCache();
4855

4956
expect(mockMkDir).toBeCalled();
50-
mockMkDir.mockRestore();
51-
mockHomedir.mockRestore();
5257
});
5358

5459
it("Load file on creation if available", () => {

src/plugins/login/utils/simpleFileTokenCache.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ import path from "path";
33
import os from "os";
44
import * as adal from "adal-node";
55

6-
let CONFIG_DIRECTORY = path.join(os.homedir(), ".azure");
7-
const DEFAULT_SLS_TOKEN_FILE = path.join(CONFIG_DIRECTORY, "slsTokenCache.json");
6+
// let CONFIG_DIRECTORY = path.join(os.homedir(), ".azure");
7+
// const DEFAULT_SLS_TOKEN_FILE = path.join(CONFIG_DIRECTORY, "slsTokenCache.json");
88

99
export class SimpleFileTokenCache implements adal.TokenCache {
1010
private entries: any[] = [];
1111
private subscriptions: any[] = [];
1212

13-
public constructor(private tokenPath: string = DEFAULT_SLS_TOKEN_FILE) {
13+
public constructor(private tokenPath: string = path.join(path.join(os.homedir(), ".azure"), "slsTokenCache.json")) {
14+
const CONFIG_DIRECTORY = path.join(os.homedir(), ".azure");
15+
const DEFAULT_SLS_TOKEN_FILE = path.join(CONFIG_DIRECTORY, "slsTokenCache.json");
1416
if(tokenPath === DEFAULT_SLS_TOKEN_FILE && !fs.existsSync(CONFIG_DIRECTORY)) {
15-
CONFIG_DIRECTORY = path.join(os.homedir(), ".azure");
16-
this.tokenPath = CONFIG_DIRECTORY;
17+
// CONFIG_DIRECTORY = path.join(os.homedir(), ".azure");
18+
// this.tokenPath = CONFIG_DIRECTORY;
1719
fs.mkdirSync(CONFIG_DIRECTORY);
1820
}
1921
this.load();

0 commit comments

Comments
 (0)