Skip to content

Commit

Permalink
Start using actual un-mocked files in config specs
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Apr 18, 2022
1 parent 7c84b4e commit aa1815e
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 37 deletions.
14 changes: 5 additions & 9 deletions lib/config.js
@@ -1,7 +1,5 @@
import { cosmiconfigSync } from 'cosmiconfig';
import parseJson from 'parse-json';
import parseToml from '@iarna/toml/parse-string.js';
import yaml from 'yaml';
import _ from 'lodash';
import isCI from 'is-ci';
import _debug from 'debug';
Expand All @@ -21,19 +19,17 @@ const searchPlaces = [
];

const loaders = {
'.json': (_, content) => parseJson(content),
'.toml': (_, content) => parseToml(content),
'.yaml': (_, content) => yaml.parse(content)
'.toml': (_, content) => parseToml(content)
};

const getLocalConfig = localConfigFile => {
const getLocalConfig = ({ file, dir = process.cwd() }) => {
let localConfig = {};
if (localConfigFile === false) return localConfig;
if (file === false) return localConfig;
const explorer = cosmiconfigSync('release-it', {
searchPlaces,
loaders
});
const result = localConfigFile ? explorer.load(localConfigFile) : explorer.search();
const result = file ? explorer.load(file) : explorer.search(dir);
if (result && typeof result.config === 'string') {
throw new Error(`Invalid configuration file at ${result.filepath}`);
}
Expand All @@ -44,7 +40,7 @@ const getLocalConfig = localConfigFile => {
class Config {
constructor(config = {}) {
this.constructorConfig = config;
this.localConfig = getLocalConfig(config.config);
this.localConfig = getLocalConfig({ file: config.config, dir: config.configDir });
this.options = this.mergeOptions();
this.options = this.expandPreReleaseShorthand(this.options);
this.contextOptions = {};
Expand Down
2 changes: 0 additions & 2 deletions package.json
Expand Up @@ -78,7 +78,6 @@
"open": "8.4.0",
"ora": "6.1.0",
"os-name": "5.0.1",
"parse-json": "5.2.0",
"promise.allsettled": "1.0.5",
"proxy-agent": "5.0.0",
"semver": "7.3.7",
Expand All @@ -87,7 +86,6 @@
"url-join": "5.0.0",
"uuid": "8.3.2",
"wildcard-match": "5.1.2",
"yaml": "1.10.2",
"yargs-parser": "21.0.1"
},
"devDependencies": {
Expand Down
45 changes: 19 additions & 26 deletions test/config.js
@@ -1,30 +1,30 @@
import test from 'ava';
import mock from 'mock-fs';
import isCI from 'is-ci';
import Config from '../lib/config.js';
import { readJSON } from '../lib/util.js';

const defaultConfig = readJSON(new URL('../config/release-it.json', import.meta.url));
const projectConfig = readJSON(new URL('../.release-it.json', import.meta.url));

const localConfig = { github: { release: true } };

test.afterEach(() => mock.restore()); // eslint-disable-line ava/no-inline-assertions

test('should contain default values', t => {
mock({ '../.release-it.json': JSON.stringify(localConfig) });
test("should read this project's own configuration", t => {
const config = new Config();
t.deepEqual(config.constructorConfig, {});
t.deepEqual(config.localConfig, projectConfig);
t.deepEqual(config.defaultConfig, defaultConfig);
});

test('should contain default values', t => {
const config = new Config({ configDir: './test/stub/config/default' });
t.deepEqual(config.constructorConfig, { configDir: './test/stub/config/default' });
t.deepEqual(config.localConfig, localConfig);
t.deepEqual(config.defaultConfig, defaultConfig);
mock.restore();
});

test('should merge provided options', t => {
mock({
'package.json': JSON.stringify({ 'release-it': { git: { push: false } } }),
'../.release-it.json': JSON.stringify(localConfig)
});
const config = new Config({
configDir: './test/stub/config/merge',
increment: '1.0.0',
verbose: true,
github: {
Expand All @@ -37,7 +37,6 @@ test('should merge provided options', t => {
t.is(options.increment, '1.0.0');
t.is(options.git.push, false);
t.is(options.github.release, true);
mock.restore();
});

test('should set CI mode', t => {
Expand All @@ -57,40 +56,34 @@ test('should override --no-npm.publish', t => {
});

test('should read YAML config', t => {
mock({ '.release-it.yaml': 'foo:\n bar: 1' });
const config = new Config({ config: '.release-it.yaml' });
const config = new Config({ configDir: './test/stub/config/yaml' });
t.deepEqual(config.options.foo, { bar: 1 });
mock.restore();
});

test('should read YML config', t => {
mock({ '.release-it.yml': 'foo:\n bar: 1' });
const config = new Config({ config: '.release-it.yml' });
const config = new Config({ configDir: './test/stub/config/yml' });
t.deepEqual(config.options.foo, { bar: 1 });
mock.restore();
});

test('should read TOML config', t => {
mock({ '.release-it.toml': '[foo]\nbar=1' });
const config = new Config({ config: '.release-it.toml' });
const config = new Config({ configDir: './test/stub/config/toml' });
t.deepEqual(config.options.foo, { bar: 1 });
mock.restore();
});

test('should throw if provided config file is not found', t => {
t.throws(() => new Config({ config: 'nofile' }), { message: /no such file.+nofile/ });
});

test('should throw if provided config file is invalid (cosmiconfig exception)', t => {
mock({ 'invalid-config-txt': 'foo\nbar\baz' });
t.throws(() => new Config({ config: 'invalid-config-txt' }), { message: /Invalid configuration file at/ });
mock.restore();
t.throws(() => new Config({ config: './test/stub/config/invalid-config-txt' }), {
message: /Invalid configuration file at/
});
});

test('should throw if provided config file is invalid (no object)', t => {
mock({ 'invalid-config-rc': 'foo=bar' });
t.throws(() => new Config({ config: 'invalid-config-rc' }), { message: /Invalid configuration file at/ });
mock.restore();
t.throws(() => new Config({ config: './test/stub/config/invalid-config-rc' }), {
message: /Invalid configuration file at/
});
});

test('should not set default increment (for CI mode)', t => {
Expand Down
5 changes: 5 additions & 0 deletions test/stub/config/default/.release-it.json
@@ -0,0 +1,5 @@
{
"github": {
"release": true
}
}
1 change: 1 addition & 0 deletions test/stub/config/invalid-config-rc
@@ -0,0 +1 @@
foo=bar
2 changes: 2 additions & 0 deletions test/stub/config/invalid-config-txt
@@ -0,0 +1,2 @@
foo
bar\baz
5 changes: 5 additions & 0 deletions test/stub/config/merge/.release-it.json
@@ -0,0 +1,5 @@
{
"github": {
"release": true
}
}
7 changes: 7 additions & 0 deletions test/stub/config/merge/package.json
@@ -0,0 +1,7 @@
{
"release-it": {
"git": {
"push": false
}
}
}
2 changes: 2 additions & 0 deletions test/stub/config/toml/.release-it.toml
@@ -0,0 +1,2 @@
[foo]
bar=1
2 changes: 2 additions & 0 deletions test/stub/config/yaml/.release-it.yaml
@@ -0,0 +1,2 @@
foo:
bar: 1
2 changes: 2 additions & 0 deletions test/stub/config/yml/.release-it.yml
@@ -0,0 +1,2 @@
foo:
bar: 1

0 comments on commit aa1815e

Please sign in to comment.