forked from jmoalves/levain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create action: checkEnvExists (issue jmoalves#107)
- Loading branch information
Pedro Henrique Rausch Bello
committed
Mar 9, 2021
1 parent
6c2162f
commit 94e9d40
Showing
5 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {assert, assertEquals, assertNotEquals, assertThrowsAsync} from "https://deno.land/std/testing/asserts.ts"; | ||
import * as path from "https://deno.land/std/path/mod.ts"; | ||
|
||
import TestHelper from "../../lib/test/test_helper.ts"; | ||
import ActionFactory from "../action_factory.ts"; | ||
import CheckFileExists from "./check_file_exists.ts"; | ||
|
||
Deno.test('CheckFileExists should be obtainable from actionFactory', () => { | ||
const config = TestHelper.getConfig() | ||
const factory = new ActionFactory() | ||
const action = factory.get("checkFileExists", config) | ||
|
||
assert(action instanceof CheckFileExists) | ||
}) | ||
Deno.test('CheckFileExists should throw error when param list is empty', async () => { | ||
const action = new CheckFileExists(TestHelper.getConfig()) | ||
const params: string[] = [] | ||
|
||
await assertThrowsAsync( | ||
async () => { | ||
await action.execute(TestHelper.mockPackage(), params) | ||
}, | ||
Error, | ||
`You must inform at least one filename to check` | ||
) | ||
}) | ||
Deno.test('CheckFileExists should throw error when param list contains only dirs', async () => { | ||
const action = new CheckFileExists(TestHelper.getConfig()) | ||
const params = [TestHelper.folderThatAlwaysExists, TestHelper.folderThatDoesNotExist] | ||
|
||
await assertThrowsAsync( | ||
async () => { | ||
await action.execute(TestHelper.mockPackage(), params) | ||
}, | ||
Error, | ||
`None of the informed files exist` | ||
) | ||
}) | ||
Deno.test('CheckFileExists should return when at least one file exists', async () => { | ||
const action = new CheckFileExists(TestHelper.getConfig()) | ||
const params = [TestHelper.fileThatDoesNotExist, TestHelper.anotherFileThatDoesNotExist, TestHelper.fileThatExists] | ||
|
||
await action.execute(TestHelper.mockPackage(), params) | ||
}) | ||
Deno.test('CheckFileExists should return when all files exists', async () => { | ||
const action = new CheckFileExists(TestHelper.getConfig()) | ||
const params = [TestHelper.fileThatExists, TestHelper.anotherFileThatExists] | ||
|
||
await action.execute(TestHelper.mockPackage(), params) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as log from "https://deno.land/std/log/mod.ts"; | ||
|
||
import Config from "../../lib/config.ts"; | ||
import Package from '../../lib/package/package.ts'; | ||
import {parseArgs} from "../../lib/parse_args.ts"; | ||
|
||
import Action from "../action.ts"; | ||
|
||
export default class CheckFileExists implements Action { | ||
constructor(private config: Config) { | ||
} | ||
|
||
// deno-lint-ignore require-await | ||
async execute(pkg: Package | undefined, parameters: string[]): Promise<void> { | ||
const args = parseArgs(parameters, {}) | ||
|
||
if (!args._ || args._.length < 1) { | ||
throw new Error(`You must inform at least one filename to check`) | ||
} | ||
|
||
const files: string[] = args._ | ||
log.info(`CHECK-FILE-EXISTS ${files.toString()}`) | ||
|
||
let fileExists = false | ||
const promises = files.map(async (file) => { | ||
try { | ||
let fileStat = await Deno.stat(file) | ||
log.debug(`${file} - ${fileStat.isFile}`) | ||
if (fileStat.isFile) fileExists = true | ||
else log.debug(`NOT A FILE: ${file}`) | ||
|
||
} catch (error) { | ||
log.debug(`NOT FOUND: ${file}`) | ||
} | ||
} | ||
); | ||
|
||
await Promise.all(promises); | ||
|
||
if (fileExists) return | ||
|
||
throw new Error(`None of the informed files exist - ${files.toString()}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters