From e9e6bd818d6832f4b2d2326c3cfb29ad10f1acc5 Mon Sep 17 00:00:00 2001 From: Choro Abdymanapov Date: Wed, 17 Jul 2019 11:12:25 +0300 Subject: [PATCH] fix: check `src` dir existence closes #44 --- src/main/ts/index.ts | 5 ++++ src/test/ts/index.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/main/ts/index.ts b/src/main/ts/index.ts index 63cd3b1..2a5b1ae 100644 --- a/src/main/ts/index.ts +++ b/src/main/ts/index.ts @@ -1,6 +1,7 @@ /** @module semantic-release-gh-pages-plugin */ import AggregateError from 'aggregate-error' +import fs from 'fs' import { TContext } from './interface' import { resolveConfig } from './config' import { publish as ghpagesPublish } from './ghpages' @@ -25,6 +26,10 @@ export const verifyConditions = async (pluginConfig: any, context: TContext) => throw new AggregateError(['package.json repository.url does not match github.com pattern']) } + if (!fs.existsSync(config.src) || !fs.lstatSync(config.src).isDirectory()) { + throw new Error('docs source directory does not exist') + } + Object.assign(pluginConfig, config) _config = config diff --git a/src/test/ts/index.ts b/src/test/ts/index.ts index ef0f8dc..eb633b4 100644 --- a/src/test/ts/index.ts +++ b/src/test/ts/index.ts @@ -1,4 +1,5 @@ import AggregateError from 'aggregate-error' +import fs from 'fs' import { TAnyMap } from '../../main/ts/interface' import { DEFAULT_SRC, @@ -10,6 +11,26 @@ import { } from '../../main/ts/defaults' import { getUrlFromPackage } from '../../main/ts/config' +beforeAll(() => { + if (!fs.existsSync(DEFAULT_SRC)) { + fs.mkdirSync(DEFAULT_SRC) + } + + if (!fs.existsSync('error')) { + fs.mkdirSync('error') + } + + if (!fs.existsSync('testFile')) { + fs.closeSync(fs.openSync('testFile', 'w')) + } +}) + +afterAll(() => { + fs.rmdirSync(DEFAULT_SRC) + fs.rmdirSync('error') + fs.unlinkSync('testFile') +}) + describe('index', () => { const log = jest.fn((...vars: any[]) => { console.log(vars) }) const error = jest.fn((...vars: any[]) => { console.log(vars) }) @@ -223,5 +244,39 @@ describe('index', () => { expect(e).toEqual(expected) } }) + + it('throws an error when docs directory does not exist', async () => { + const { publish } = require('../../main/ts') + const context = { + logger, + options: { + ...globalConfig, + [step]: [{ path, src: 'notExistingDirectory' }] + }, + env: { GITHUB_TOKEN: token + 'foo' } + } + try { + await publish({}, context) + } catch (e) { + expect(e.message).toBe('docs source directory does not exist') + } + }) + + it('throws an error when docs is a file rather than directory', async () => { + const { publish } = require('../../main/ts') + const context = { + logger, + options: { + ...globalConfig, + [step]: [{ path, src: 'testFile' }] + }, + env: { GITHUB_TOKEN: token + 'foo' } + } + try { + await publish({}, context) + } catch (e) { + expect(e.message).toBe('docs source directory does not exist') + } + }) }) })