Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracts config.json into its own module #1061

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Adds configCore and configCoreFileFormats tests
  • Loading branch information
Spoffy committed Jun 25, 2024
commit 9276595d9b118b285a3d8bbf190c1fb36acfc89d
3 changes: 2 additions & 1 deletion test/server/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from 'chai';
import * as sinon from 'sinon';
import { FileConfig, Deps, createConfigValue, ConfigAccessors } from "app/server/lib/config";
import { ConfigAccessors, createConfigValue, Deps, FileConfig } from "app/server/lib/config";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put the imports in alphabetical order, and keep the quoting locally consistent. If you know I'm going to review, it can be a good idea to pre-review your own code for my import foibles: alphabetical, and consistent.

Copy link
Contributor Author

@Spoffy Spoffy Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do! I'm stuck in bad habits, too used to having a linter around that auto-formats all this! 😆


interface TestFileContents {
myNum?: number
Expand All @@ -23,6 +23,7 @@ describe('FileConfig', () => {
fakeFile.contents = newContents;
return Promise.resolve();
}));

return fakeFile;
};

Expand Down
30 changes: 30 additions & 0 deletions test/server/lib/configCore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as sinon from 'sinon';
import { assert } from 'chai';
import { loadGristCoreConfig, loadGristCoreConfigFile } from "app/server/lib/configCore";
import { Deps } from "app/server/lib/config";

describe('loadGristCoreConfig', () => {
afterEach(() => {
sinon.restore();
});

it('can be used with an in-memory store if no file config is provided', async () => {
const config = loadGristCoreConfig();
await config.edition.set("enterprise");
assert.equal(config.edition.get(), "enterprise");
});

it('will function correctly when no config file is present', async () => {
sinon.replace(Deps, 'pathExists', sinon.fake.resolves(false));
sinon.replace(Deps, 'readFile', sinon.fake.resolves(""));
const writeFileFake = sinon.fake.resolves(undefined);
sinon.replace(Deps, 'writeFile', writeFileFake);

const config = await loadGristCoreConfigFile("doesntmatter.json");
assert.exists(config.edition.get());

await config.edition.set("enterprise");
// Make sure that the change was written back to the file.
assert.isTrue(writeFileFake.calledOnce);
});
});
29 changes: 29 additions & 0 deletions test/server/lib/configCoreFileFormats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { assert } from 'chai';
import { convertToCoreFileContents, IGristCoreConfigFileLatest } from "app/server/lib/configCoreFileFormats";

describe('convertToCoreFileContents', () => {
it('fails with a malformed config', async () => {
const badConfig = {
version: "This is a random version number that will never exist",
};

assert.isNull(convertToCoreFileContents(badConfig));
});

// This is necessary to handle users who don't have a config file yet.
it('will upgrade an empty object to a valid config', () => {
const validConfig = convertToCoreFileContents({});
assert.exists(validConfig?.version);
});

it('will validate the latest config file format', () => {
const validRawObject: IGristCoreConfigFileLatest = {
version: "1",
edition: "enterprise",
};

const validConfig = convertToCoreFileContents(validRawObject);
assert.exists(validConfig?.version);
assert.exists(validConfig?.edition);
});
});
Loading