Skip to content

Commit

Permalink
fix: check src dir existence
Browse files Browse the repository at this point in the history
closes #44
  • Loading branch information
oljekechoro authored and antongolub committed Jul 17, 2019
1 parent 7cefadb commit e9e6bd8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down
55 changes: 55 additions & 0 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AggregateError from 'aggregate-error'
import fs from 'fs'
import { TAnyMap } from '../../main/ts/interface'
import {
DEFAULT_SRC,
Expand All @@ -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) })
Expand Down Expand Up @@ -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')
}
})
})
})

0 comments on commit e9e6bd8

Please sign in to comment.