Skip to content

Commit

Permalink
Fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Aug 1, 2021
1 parent c97fc2b commit 7a76101
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 62 deletions.
77 changes: 24 additions & 53 deletions test/plugins.js
Expand Up @@ -12,6 +12,8 @@ import ContextPlugin from './stub/plugin-context.js';
import { mkTmpDir } from './util/helpers.js';
import ShellStub from './stub/shell.js';

const rootDir = new URL('..', import.meta.url);

const noop = Promise.resolve();

const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -48,22 +50,24 @@ test.serial.afterEach(() => {
});

test.serial('should instantiate plugins and execute all release-cycle methods', async t => {
sh.exec('npm init -f');
sh.ShellString(JSON.stringify({ name: 'project', version: '1.0.0', type: 'module' })).toEnd('package.json');
sh.exec(`npm install ${rootDir}`);

sh.mkdir('my-plugin');
sh.pushd('-q', 'my-plugin');
sh.exec('npm init -f');
sh.exec('npm install release-it');
sh.ShellString("const { Plugin } = require('release-it'); module.exports = " + MyPlugin.toString()).toEnd('index.js');
const pluginDir = mkTmpDir();
sh.pushd('-q', pluginDir);
sh.ShellString(JSON.stringify({ name: 'my-plugin', version: '1.0.0', type: 'module' })).toEnd('package.json');
sh.exec(`npm install ${rootDir}`);
const content = "import { Plugin } from 'release-it'; " + MyPlugin.toString() + '; export default MyPlugin;';
sh.ShellString(content).toEnd('index.js');
sh.popd();

sh.mkdir('-p', 'my/plugin');
sh.pushd('-q', 'my/plugin');
sh.exec('npm init -f');
sh.exec('npm install release-it');
sh.ShellString("const { Plugin } = require('release-it'); module.exports = " + MyPlugin.toString()).toEnd('index.js');
sh.ShellString(content).toEnd('index.js');
sh.popd();

sh.exec(`npm install ${pluginDir}`);

const config = {
plugins: {
'my-plugin': {
Expand Down Expand Up @@ -110,18 +114,14 @@ test.serial('should instantiate plugins and execute all release-cycle methods',
});

test.serial('should disable core plugins', async t => {
sh.exec('npm init -f');
sh.mkdir('replace-plugin');
sh.pushd('-q', 'replace-plugin');
sh.exec('npm init -f');
sh.exec('npm install release-it');
sh.ShellString(JSON.stringify({ name: 'project', version: '1.0.0' })).toEnd('package.json');
sh.exec(`npm install release-it@^14`);
const content = "const { Plugin } = require('release-it'); module.exports = " + ReplacePlugin.toString();
sh.ShellString(content).toEnd('index.js');
sh.popd();
sh.ShellString(content).toEnd('replace-plugin.js');

const config = {
plugins: {
'replace-plugin': {}
'./replace-plugin.js': {}
}
};
const container = getContainer(config);
Expand All @@ -136,46 +136,17 @@ test.serial('should disable core plugins', async t => {
});
});

test.serial('should support ESM-based plugins', async t => {
sh.exec('npm init -f');
sh.mkdir('my-plugin');
sh.pushd('-q', 'my-plugin');
sh.ShellString('{"name":"my-plugin","version":"1.0.0","type": "module"}').toEnd('package.json');
sh.exec('npm install release-it@esm');
const content = "import { Plugin } from 'release-it'; " + MyPlugin.toString() + '; export default MyPlugin;';
sh.ShellString(content).toEnd('index.js');
sh.popd();

const config = {
plugins: {
'my-plugin': {}
}
};
const container = getContainer(config);

const result = await runTasks({}, container);
test.serial('should expose context to execute commands', async t => {
sh.ShellString(JSON.stringify({ name: 'pkg-name', version: '1.0.0', type: 'module' })).toEnd('package.json');
sh.exec(`npm install ${rootDir}`);

t.deepEqual(result, {
changelog: undefined,
name: 'new-project-name',
latestVersion: '1.2.3',
version: '1.3.0'
});
});
const content =
"import { Plugin } from 'release-it'; " + ContextPlugin.toString() + '; export default ContextPlugin;';
sh.ShellString(content).toEnd('context-plugin.js');

test.serial('should expose context to execute commands', async t => {
sh.ShellString('{"name":"pkg-name","version":"1.0.0"}').toEnd('package.json');
const repo = parseGitUrl('https://github.com/user/pkg');

sh.mkdir('context-plugin');
sh.pushd('-q', 'context-plugin');
sh.exec('npm init -f');
sh.exec('npm install release-it');
const content = "const { Plugin } = require('release-it'); module.exports = " + ContextPlugin.toString();
sh.ShellString(content).toEnd('index.js');
sh.popd();

const container = getContainer({ plugins: { 'context-plugin': {} } });
const container = getContainer({ plugins: { './context-plugin.js': {} } });
const exec = sinon.spy(container.shell, 'execFormattedCommand');

container.config.setContext({ repo });
Expand Down
20 changes: 11 additions & 9 deletions test/tasks.js
Expand Up @@ -22,6 +22,8 @@ import {
interceptAsset as interceptGitHubAsset
} from './stub/github.js';

const rootDir = new URL('..', import.meta.url);

const noop = Promise.resolve();

const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -397,14 +399,10 @@ test.serial('should propagate errors', async t => {

{
test.serial('should run all hooks', async t => {
gitAdd(`{"name":"hooked","version":"1.0.0"}`, 'package.json', 'Add package.json');
sh.mkdir('my-plugin');
sh.pushd('-q', 'my-plugin');
sh.exec('npm init -f');
sh.exec('npm install release-it');
const plugin = "const { Plugin } = require('release-it'); module.exports = class MyPlugin extends Plugin {};";
sh.ShellString(plugin).toEnd('index.js');
sh.popd();
gitAdd(`{"name":"hooked","version":"1.0.0","type":"module"}`, 'package.json', 'Add package.json');
sh.exec(`npm install ${rootDir}`);
const plugin = "import { Plugin } from 'release-it'; class MyPlugin extends Plugin {}; export default MyPlugin;";
sh.ShellString(plugin).toEnd('my-plugin.js');

const hooks = {};
['before', 'after'].forEach(prefix => {
Expand All @@ -415,7 +413,11 @@ test.serial('should propagate errors', async t => {
});
});
});
const container = getContainer({ plugins: { 'my-plugin': {} }, hooks });
const container = getContainer({
plugins: { './my-plugin.js': {} },
git: { requireCleanWorkingDir: false },
hooks
});
const exec = sinon.spy(container.shell, 'execFormattedCommand');

await runTasks({}, container);
Expand Down

0 comments on commit 7a76101

Please sign in to comment.