Skip to content

Commit

Permalink
Create action: checkEnvExists (issue jmoalves#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Henrique Rausch Bello committed Mar 9, 2021
1 parent 6c2162f commit 94e9d40
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/action/action_factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Deno.test('ActionFactory should list actions', () => {
'setEnv',
'template',
'checkChainDirExists',
'checkFileExists',
'echo',
'removeFromRegistry',
'setVar',
Expand Down
2 changes: 2 additions & 0 deletions src/action/action_factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Action from "./action.ts";
import AddPathAction from "./add_path.ts";
import CheckFileExists from "./check/check_file_exists.ts";
import CopyAction from "./copy.ts";
import ContextMenu from "./context_menu.ts";
import DefaultPackage from "./defaultPackage.ts";
Expand Down Expand Up @@ -41,6 +42,7 @@ const actionMap = new Map<string, (config: Config) => Action>([
['template', (config: Config) => new Template(config)],
['assertContains', (config: Config) => new AssertContainsAction(config)],
['checkChainDirExists', (config: Config) => new CheckChainDirExists(config)],
['checkFileExists', (config: Config) => new CheckFileExists(config)],
['echo', (config: Config) => new Echo(config)],
['removeFromRegistry', (config: Config) => new RemoveFromRegistry(config.levainRegistry)],
['setVar', (config: Config) => new SetVarAction(config)],
Expand Down
51 changes: 51 additions & 0 deletions src/action/check/check_file_exists.test.ts
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)
})

44 changes: 44 additions & 0 deletions src/action/check/check_file_exists.ts
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()}`)
}
}
3 changes: 3 additions & 0 deletions src/lib/test/test_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export default class TestHelper {
static readonly folderThatDoesNotExist = 'this-folder-does-not-exist';
static readonly anotherFolderThatDoesNotExist = 'another-folder-that-does-not-exist';
static readonly fileThatDoesNotExist = path.join(TestHelper.folderThatAlwaysExists, 'this-file-does-not-exist.txt');
static readonly anotherFileThatDoesNotExist = path.join(TestHelper.folderThatAlwaysExists, 'this-file-also-does-not-exist.txt');
static readonly fileThatExists = path.resolve('testdata/file_utils/can_read_and_write_this_file.txt');
static readonly anotherFileThatExists = path.resolve('testdata/file_utils/file.txt');
static readonly validZipFile = path.resolve('testdata/extract/test.zip')

static getTestPkg(yamlStr: string) {
Expand Down

0 comments on commit 94e9d40

Please sign in to comment.