Skip to content

Validate yaml #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
validate yaml setup
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jun 7, 2020
commit c0e7cd0c6c63e2608a936e101840c5df91551073
4 changes: 4 additions & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ async function build(args: string[]) {
let config;
try {
config = yamlParser.load(_yaml);
// TODO: validate yaml
if (!config || !config.length) {
throw new Error("Invalid yaml file contents");
}
} catch (e) {
console.error("Error parsing yaml");
console.error(e.message);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs";
import util from "util";
import * as path from "path";
import gitP, { SimpleGit } from "simple-git/promise";
import { addToCommitOrder, validateCommitOrder } from "./commitOrder";
import { validateCommitOrder } from "./validateCommits";

const mkdir = util.promisify(fs.mkdir);
const exists = util.promisify(fs.exists);
Expand Down
1 change: 0 additions & 1 deletion src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ export function parse(params: ParseParams): any {

// add init commits
if (params.commits.INIT && params.commits.INIT.length) {
console.log(JSON.stringify(parsed.config?.testRunner));
// @ts-ignore
parsed.config.testRunner.setup = {
...(parsed.config?.testRunner?.setup || {}),
Expand Down
7 changes: 1 addition & 6 deletions src/utils/commitOrder.ts → src/utils/validateCommits.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// should flag commits that are out of order based on the previous commit
// position is a string like 'INIT', 'L1', 'L1S1'
export function addToCommitOrder(position: string) {
// add position to list
}

export function validateCommitOrder(positions: string[]): boolean {
// loop over positions
const errors: number[] = [];
Expand All @@ -12,7 +8,6 @@ export function validateCommitOrder(positions: string[]): boolean {
positions.forEach((position: string, index: number) => {
if (position === "INIT") {
if (previous.level !== 0 && previous.step !== 0) {
console.log("ERROR HERE");
errors.push(index);
}
current = { level: 0, step: 0 };
Expand Down Expand Up @@ -46,7 +41,7 @@ export function validateCommitOrder(positions: string[]): boolean {
previous = current;
});

if (errors.length) {
if (errors.length && process.env.NODE_ENV !== "test") {
console.warn("Found commit positions out of order");
positions.forEach((position, index) => {
if (errors.includes(index)) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/validateYaml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function validateYaml(yaml: string) {
return true;
}
31 changes: 27 additions & 4 deletions src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
import * as path from "path";
import * as fs from "fs";
import util from "util";
import * as yamlParser from "js-yaml";
import { getArg } from "./utils/args";
import gitP, { SimpleGit } from "simple-git/promise";
import { getCommits, CommitLogObject } from "./utils/commits";

const mkdir = util.promisify(fs.mkdir);
const exists = util.promisify(fs.exists);
const rmdir = util.promisify(fs.rmdir);
const read = util.promisify(fs.readFile);

async function validate(args: string[]) {
// dir - default .
const dir = !args.length || args[0].match(/^-/) ? "." : args[0];
console.warn("Not yet implemented. Coming soon");

const localDir = path.join(process.cwd(), dir);
const codeBranch = "";

const commits = getCommits({ localDir, codeBranch });
// -y --yaml - default coderoad-config.yml
const options = {
yaml: getArg(args, { name: "yaml", alias: "y" }) || "coderoad.yaml";
}

const _yaml = await read(path.join(localDir, options.yaml), "utf8");

// parse yaml config
let config;
try {
config = yamlParser.load(_yaml)
// TODO: validate yaml
if (!config || !config.length) {
throw new Error('Invalid yaml file contents')
}
} catch (e) {
console.error("Error parsing yaml");
console.error(e.message);
}

const codeBranch: string = config.config.repo.branch;

// VALIDATE SKELETON WITH COMMITS
const commits = getCommits({ localDir, codeBranch });

// parse tutorial skeleton for order and commands

// on error, warn missing level/step
Expand Down
2 changes: 1 addition & 1 deletion tests/commitOrder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { validateCommitOrder } from "../src/utils/commitOrder";
import { validateCommitOrder } from "../src/utils/validateCommits";

describe("commitOrder", () => {
it("should return true if order is valid", () => {
Expand Down
3 changes: 3 additions & 0 deletions tests/yaml.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe("yaml", () => {
it.todo("should parse a valid yaml file");
});