Skip to content

Commit a14ca33

Browse files
committed
Tweak state loading/saving
1 parent 515973f commit a14ca33

File tree

6 files changed

+95
-72
lines changed

6 files changed

+95
-72
lines changed

.github/workflows/code_health.yaml

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
---
22
name: Code Health
33
on:
4-
push:
5-
branches:
6-
- main
7-
pull_request:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
88
jobs:
9-
check-style:
10-
runs-on: ubuntu-latest
11-
steps:
12-
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
13-
- uses: actions/checkout@v4
14-
- uses: actions/setup-node@v4
15-
with:
16-
node-version-file: package.json
17-
cache: "npm"
18-
- name: Install dependencies
19-
run: npm ci
20-
- name: Run style check
21-
run: npm run check
9+
check-style:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-node@v4
15+
with:
16+
node-version-file: package.json
17+
cache: "npm"
18+
- name: Install dependencies
19+
run: npm ci
20+
- name: Run style check
21+
run: npm run check
2222

23-
run-tests:
24-
runs-on: ubuntu-latest
25-
steps:
26-
- name: Install keyring deps
27-
run: |
28-
sudo apt update -y
29-
sudo apt install -y gnome-keyring libdbus-1-dev
23+
run-tests:
24+
strategy:
25+
matrix:
26+
os: [ubuntu-latest, macos-latest, windows-latest]
27+
runs-on: ${{ matrix.os }}
28+
steps:
29+
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
30+
- name: Install keyring deps on Ubuntu
31+
if: matrix.os == 'ubuntu-latest'
32+
run: |
33+
sudo apt update -y
34+
sudo apt install -y gnome-keyring libdbus-1-dev
3035
31-
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
32-
- uses: actions/checkout@v4
33-
- uses: actions/setup-node@v4
34-
with:
35-
node-version-file: package.json
36-
cache: "npm"
37-
- name: Install dependencies
38-
run: npm ci
39-
- name: Run tests
40-
run: npm test
36+
- uses: actions/checkout@v4
37+
- uses: actions/setup-node@v4
38+
with:
39+
node-version-file: package.json
40+
cache: "npm"
41+
- name: Install dependencies
42+
run: npm ci
43+
- name: Run tests
44+
run: npm test

.github/workflows/publish.yaml

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
---
22
name: Publish
33
on:
4-
push:
5-
tags:
6-
- v*
4+
push:
5+
tags:
6+
- v*
77
jobs:
8-
publish:
9-
runs-on: ubuntu-latest
10-
environment: Production
11-
steps:
12-
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
13-
- uses: actions/checkout@v4
14-
- uses: actions/setup-node@v4
15-
with:
16-
node-version-file: package.json
17-
registry-url: "https://registry.npmjs.org"
18-
cache: "npm"
19-
- name: Build package
20-
run: |
21-
npm ci
22-
npm run build
23-
- name: Publish to NPM
24-
run: npm publish
25-
env:
26-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
27-
- name: Publish Github release
28-
run: |
29-
gh release create ${{ github.ref }} --title "${{ github.ref }}" --notes "Release ${{ github.ref }}" --generate-notes
8+
publish:
9+
runs-on: ubuntu-latest
10+
environment: Production
11+
steps:
12+
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-node@v4
15+
with:
16+
node-version-file: package.json
17+
registry-url: "https://registry.npmjs.org"
18+
cache: "npm"
19+
- name: Build package
20+
run: |
21+
npm ci
22+
npm run build
23+
- name: Publish to NPM
24+
run: npm publish
25+
env:
26+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
27+
- name: Publish Github release
28+
run: |
29+
gh release create ${{ github.ref }} --title "${{ github.ref }}" --notes "Release ${{ github.ref }}" --generate-notes

.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
"tabWidth": 2,
1919
"printWidth": 80
2020
}
21+
},
22+
{
23+
"files": "*.yaml",
24+
"options": {
25+
"tabWidth": 2,
26+
"printWidth": 80
27+
}
2128
}
2229
]
2330
}

src/state.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ export class State {
1313
serviceProvider?: NodeDriverServiceProvider;
1414

1515
public async persistCredentials(): Promise<void> {
16-
await this.entry.setPassword(JSON.stringify(this.credentials));
16+
try {
17+
await this.entry.setPassword(JSON.stringify(this.credentials));
18+
} catch (err) {
19+
logger.error(mongoLogId(1_000_008), "state", `Failed to save state: ${err}`);
20+
}
1721
}
1822

19-
public async loadCredentials(): Promise<boolean> {
23+
public async loadCredentials(): Promise<void> {
2024
try {
2125
const data = await this.entry.getPassword();
2226
if (data) {
2327
this.credentials = JSON.parse(data);
2428
}
25-
26-
return true;
2729
} catch (err: unknown) {
2830
logger.error(mongoLogId(1_000_007), "state", `Failed to load state: ${err}`);
29-
return false;
3031
}
3132
}
3233
}

tests/integration/helpers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ import { Server } from "../../src/server.js";
44
import runner, { MongoCluster } from "mongodb-runner";
55
import path from "path";
66
import fs from "fs/promises";
7+
import defaultState from "../../src/state.js";
78

89
export async function setupIntegrationTest(): Promise<{
910
client: Client;
1011
server: Server;
12+
teardown: () => Promise<void>;
1113
}> {
14+
// Mock the load/persist credentials method to avoid state loading/restore messing up with the tests
15+
const loadCredentialsMock = jest.spyOn(defaultState, "loadCredentials").mockImplementation(() => Promise.resolve());
16+
const saveCredentialsMock = jest
17+
.spyOn(defaultState, "persistCredentials")
18+
.mockImplementation(() => Promise.resolve());
19+
1220
const clientTransport = new InMemoryTransport();
1321
const serverTransport = new InMemoryTransport();
1422

@@ -35,6 +43,13 @@ export async function setupIntegrationTest(): Promise<{
3543
return {
3644
client,
3745
server,
46+
teardown: async () => {
47+
await client.close();
48+
await server.close();
49+
50+
loadCredentialsMock.mockRestore();
51+
saveCredentialsMock.mockRestore();
52+
},
3853
};
3954
}
4055

tests/integration/tools/mongodb/connect.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ import config from "../../../../src/config.js";
99
describe("Connect tool", () => {
1010
let client: Client;
1111
let server: Server;
12+
let serverClientTeardown: () => Promise<void>;
1213

13-
let loadCredentialsMock: jest.SpyInstance | undefined;
1414
let cluster: runner.MongoCluster;
1515

1616
beforeAll(async () => {
1717
cluster = await runMongoDB();
1818
}, 60_000);
1919

2020
afterEach(async () => {
21-
loadCredentialsMock?.mockRestore();
22-
await client?.close();
23-
await server?.close();
21+
await serverClientTeardown?.();
2422
});
2523

2624
afterAll(async () => {
@@ -29,9 +27,7 @@ describe("Connect tool", () => {
2927

3028
describe("with default config", () => {
3129
beforeEach(async () => {
32-
loadCredentialsMock = jest.spyOn(defaultState, "loadCredentials").mockImplementation(async () => true);
33-
34-
({ client, server } = await setupIntegrationTest());
30+
({ client, server, teardown: serverClientTeardown } = await setupIntegrationTest());
3531
});
3632

3733
it("should have correct metadata", async () => {
@@ -92,7 +88,7 @@ describe("Connect tool", () => {
9288
beforeEach(async () => {
9389
config.connectionString = cluster.connectionString;
9490

95-
({ client, server } = await setupIntegrationTest());
91+
({ client, server, teardown: serverClientTeardown } = await setupIntegrationTest());
9692
});
9793

9894
it("uses the connection string from config", async () => {

0 commit comments

Comments
 (0)