Skip to content

Commit 1085fe0

Browse files
committed
feat(ggshield-configuration): Add test for GGshieldConfiguration
1 parent 7a1daa3 commit 1085fe0

File tree

4 files changed

+108
-40
lines changed

4 files changed

+108
-40
lines changed

src/extension.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
scanFile,
1111
showAPIQuota,
1212
} from "./lib/ggshield-api";
13-
import { getConfiguration, setApiKey } from "./lib/ggshield-configuration";
13+
import {
14+
getConfiguration,
15+
setApiKey,
16+
} from "./lib/ggshield-configuration-utils";
1417
import {
1518
ExtensionContext,
1619
Uri,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { getBinaryAbsolutePath } from "./ggshield-resolver-utils";
2+
import { ExtensionContext, workspace } from "vscode";
3+
import * as os from "os";
4+
import { GGShieldConfiguration } from "./ggshield-configuration";
5+
6+
const apiUrlDefault = "https://dashboard.gitguardian.com/";
7+
8+
/**
9+
* Retrieve configuration from settings
10+
*
11+
* TODO: Check with Mathieu if this behaviour is expected
12+
* @returns {GGShieldConfiguration} from the extension settings
13+
*/
14+
export function getConfiguration(
15+
context: ExtensionContext
16+
): GGShieldConfiguration {
17+
const config = workspace.getConfiguration("gitguardian");
18+
19+
const ggshieldPath: string | undefined = config.get("GGShieldPath");
20+
const apiUrl: string | undefined = config.get("apiUrl");
21+
const apiKey: string | undefined = config.get("apiKey");
22+
const allowSelfSigned: boolean = config.get("allowSelfSigned", false);
23+
return new GGShieldConfiguration(
24+
ggshieldPath
25+
? ggshieldPath
26+
: getBinaryAbsolutePath(os.platform(), os.arch(), context),
27+
apiUrl ? apiUrl : apiUrlDefault,
28+
apiKey ? apiKey : "",
29+
allowSelfSigned ? allowSelfSigned : false
30+
);
31+
}
32+
33+
export function setApiKey(
34+
configuration: GGShieldConfiguration,
35+
apiKey: string | undefined
36+
): void {
37+
configuration.apiKey = apiKey ? apiKey : "";
38+
}

src/lib/ggshield-configuration.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import { getBinaryAbsolutePath } from "./ggshield-resolver-utils";
2-
import { ExtensionContext, workspace } from "vscode";
3-
import * as os from "os";
4-
5-
const apiUrlDefault = "https://dashboard.gitguardian.com/";
6-
71
export class GGShieldConfiguration {
82
ggshieldPath: string;
93
apiUrl: string;
@@ -22,36 +16,3 @@ export class GGShieldConfiguration {
2216
this.allowSelfSigned = allowSelfSigned;
2317
}
2418
}
25-
26-
/**
27-
* Retrieve configuration from settings
28-
*
29-
* TODO: Check with Mathieu if this behaviour is expected
30-
* @returns {GGShieldConfiguration} from the extension settings
31-
*/
32-
export function getConfiguration(
33-
context: ExtensionContext
34-
): GGShieldConfiguration {
35-
const config = workspace.getConfiguration("gitguardian");
36-
37-
const ggshieldPath: string | undefined = config.get("GGShieldPath");
38-
const apiUrl: string | undefined = config.get("apiUrl");
39-
const apiKey: string | undefined = config.get("apiKey");
40-
const allowSelfSigned: boolean = config.get("allowSelfSigned", false);
41-
42-
return new GGShieldConfiguration(
43-
ggshieldPath
44-
? ggshieldPath
45-
: getBinaryAbsolutePath(os.platform(), os.arch(), context),
46-
apiUrl ? apiUrl : apiUrlDefault,
47-
apiKey ? apiKey : "",
48-
allowSelfSigned ? allowSelfSigned : false
49-
);
50-
}
51-
52-
export function setApiKey(
53-
configuration: GGShieldConfiguration,
54-
apiKey: string | undefined
55-
): void {
56-
configuration.apiKey = apiKey ? apiKey : "";
57-
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as simple from "simple-mock";
2+
import assert = require("assert");
3+
import { ExtensionContext, workspace } from "vscode";
4+
import * as GGShieldConfiguration from "../../../lib/ggshield-configuration";
5+
import { getConfiguration } from "../../../lib/ggshield-configuration-utils";
6+
7+
suite("getConfiguration", () => {
8+
let getConfigurationMock: simple.Stub<Function>;
9+
let constructorMock: simple.Stub<Function>;
10+
let context: Partial<ExtensionContext>;
11+
12+
setup(() => {
13+
// Mock workspace.getConfiguration
14+
getConfigurationMock = simple.mock(workspace, "getConfiguration");
15+
16+
// Mock the GGShieldConfiguration constructor globally
17+
constructorMock = simple.mock(
18+
GGShieldConfiguration,
19+
"GGShieldConfiguration"
20+
);
21+
context = {} as ExtensionContext;
22+
});
23+
24+
teardown(() => {
25+
simple.restore();
26+
});
27+
28+
test("Vscode settings are correctly read", async () => {
29+
getConfigurationMock.returnWith({
30+
get: (key: string) => {
31+
if (key === "GGShieldPath") {
32+
return "path/to/ggshield";
33+
}
34+
if (key === "apiUrl") {
35+
return "https://custom-url.com";
36+
}
37+
if (key === "apiKey") {
38+
return "test-api-key";
39+
}
40+
if (key === "allowSelfSigned") {
41+
return true;
42+
}
43+
},
44+
});
45+
constructorMock.returnWith({});
46+
47+
getConfiguration(context as ExtensionContext);
48+
49+
// Assert both workspace.getConfiguration and GGShieldConfiguration constructor were called
50+
assert(
51+
getConfigurationMock.called,
52+
"getConfiguration should be called once"
53+
);
54+
assert(
55+
constructorMock.called,
56+
"GGShieldConfiguration constructor should be called once"
57+
);
58+
59+
// Check the arguments passed to GGShieldConfiguration constructor
60+
const ggshieldConfigurationArgs = constructorMock.lastCall.args;
61+
assert.strictEqual(ggshieldConfigurationArgs[0], "path/to/ggshield");
62+
assert.strictEqual(ggshieldConfigurationArgs[1], "https://custom-url.com");
63+
assert.strictEqual(ggshieldConfigurationArgs[2], "test-api-key");
64+
assert.strictEqual(ggshieldConfigurationArgs[3], true);
65+
});
66+
});

0 commit comments

Comments
 (0)