Files
vscode-codeql/extensions/ql-vscode/test/pure-tests/databases/db-config-store.test.ts
Shati Patel 7296c645b9 Add database configuration store (#1691)
This "config store" creates a `dbconfig.json` file (if it doesn't yet exist),
and reads the file to load the database panel state.

Only the database config store should be able to modify the config
— the config cannot be modified externally.
2022-11-02 15:07:23 +00:00

58 lines
1.9 KiB
TypeScript

import * as fs from 'fs-extra';
import * as path from 'path';
import { DbConfigStore } from '../../../src/databases/db-config-store';
import { expect } from 'chai';
describe('db config store', async () => {
const workspaceStoragePath = path.join(__dirname, 'test-workspace');
const testStoragePath = path.join(__dirname, 'data');
beforeEach(async () => {
await fs.ensureDir(workspaceStoragePath);
});
afterEach(async () => {
await fs.remove(workspaceStoragePath);
});
it('should create a new config if one does not exist', async () => {
const configPath = path.join(workspaceStoragePath, 'dbconfig.json');
const configStore = new DbConfigStore(workspaceStoragePath);
await configStore.initialize();
expect(await fs.pathExists(configPath)).to.be.true;
const config = configStore.getConfig();
expect(config.remote.repositoryLists).to.be.empty;
expect(config.remote.owners).to.be.empty;
expect(config.remote.repositories).to.be.empty;
});
it('should load an existing config', async () => {
const configStore = new DbConfigStore(testStoragePath);
await configStore.initialize();
const config = configStore.getConfig();
expect(config.remote.repositoryLists).to.have.length(1);
expect(config.remote.repositoryLists[0]).to.deep.equal({
'name': 'repoList1',
'repositories': ['foo/bar', 'foo/baz']
});
expect(config.remote.owners).to.be.empty;
expect(config.remote.repositories).to.have.length(3);
expect(config.remote.repositories).to.deep.equal(['owner/repo1', 'owner/repo2', 'owner/repo3']);
});
it('should not allow modification of the config', async () => {
const configStore = new DbConfigStore(testStoragePath);
await configStore.initialize();
const config = configStore.getConfig();
config.remote.repositoryLists = [];
const reRetrievedConfig = configStore.getConfig();
expect(reRetrievedConfig.remote.repositoryLists).to.have.length(1);
});
});