Skip to content

Commit

Permalink
fix(legacy): prevented error when attempting to create hook when dire…
Browse files Browse the repository at this point in the history
…ctory does not exist

when the project still uses a legacy version of husky
  • Loading branch information
travi committed Aug 4, 2022
1 parent 99b36df commit 21d272c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
19 changes: 16 additions & 3 deletions src/hook-creator-test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import {promises as fs} from 'fs';
import * as core from '@form8ion/core';

import sinon from 'sinon';
import {assert} from 'chai';
import any from '@travi/any';

import createHook from './hook-creator';

suite('hook creator', () => {
let sandbox;
const configDirectory = any.string();
const hookName = any.word();
const script = any.string();

setup(() => {
sandbox = sinon.createSandbox();

sandbox.stub(fs, 'writeFile');
sandbox.stub(core, 'directoryExists');
});

teardown(() => sandbox.restore());

test('that a hook file is created', async () => {
const configDirectory = any.string();
const hookName = any.word();
const script = any.string();
core.directoryExists.withArgs(configDirectory).resolves(true);

await createHook({configDirectory, hookName, script});

Expand All @@ -32,4 +37,12 @@ ${script}`,
{mode: 0o755}
);
});

test('that the hook file is not created if the config directory does not exist', async () => {
core.directoryExists.resolves(false);

await createHook({configDirectory, hookName, script});

assert.notCalled(fs.writeFile);
});
});
13 changes: 8 additions & 5 deletions src/hook-creator.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {promises as fs} from 'fs';
import {directoryExists} from '@form8ion/core';

export default async function ({configDirectory, hookName, script}) {
await fs.writeFile(
`${configDirectory}/${hookName}`,
`#!/bin/sh
if (await directoryExists(configDirectory)) {
await fs.writeFile(
`${configDirectory}/${hookName}`,
`#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
${script}`,
{mode: 0o755}
);
{mode: 0o755}
);
}
}
1 change: 1 addition & 0 deletions test/integration/features/lift.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Feature: Husky

Scenario: Husky v4 installed, v4 config
Given husky v4 is installed
And commitlint is configured for the project
And husky config is in v4 format
When the husky details are lifted
Then the next-steps do not include a warning about the husky config

0 comments on commit 21d272c

Please sign in to comment.