Skip to content

Commit

Permalink
Merge pull request #59 from marmelab/test-config-file
Browse files Browse the repository at this point in the history
Add controls on config file
  • Loading branch information
erwanMarmelab committed Sep 4, 2023
2 parents 62dab4d + 3ae018d commit 66a3f3a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/commands/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ERROR_CODES from '../services/errors/errorCodes';
import logErrorOnSentry from '../services/errors/Sentry';

import { DEFAULT_SAMPLES } from '../constants';
export const DEFAULT_CONFIG_FILE = './.greenframe.yml';

import createNewAnalysis from '../tasks/createNewAnalysis';
import detectDockerVersion from '../tasks/detectDockerVersion';
Expand All @@ -34,7 +35,7 @@ class AnalyzeCommand extends Command {
];

static defaultFlags = {
configFile: './.greenframe.yml',
configFile: DEFAULT_CONFIG_FILE,
samples: DEFAULT_SAMPLES,
distant: false,
useAdblock: false,
Expand Down
4 changes: 2 additions & 2 deletions src/examples/greenframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const greenframe = async (page) => {
page.waitForNavigation({
/* url: 'https://greenframe.io/', */ waitUntil: 'networkidle',
}),
page.scrollToElement('text=Try for free'),
page.click('text=Try for free'),
page.scrollToElement('text=Try it for free'),
page.click('text=Try it for free'),
]);
await page.addMilestone('Go Back home');

Expand Down
27 changes: 24 additions & 3 deletions src/services/parseConfigFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ const fs = require('node:fs');
const yaml = require('js-yaml');
const util = require('node:util');
const ConfigurationError = require('./errors/ConfigurationError');
const analyze = require('../commands/analyze');
const FILE_NOT_FOUND = 'ENOENT';

const readFile = util.promisify(fs.readFile);

const isMissingDefaultConfigFile = (path, error) => {
return path === analyze.DEFAULT_CONFIG_FILE && error.code === FILE_NOT_FOUND;
};

const parseConfigFile = async (path) => {
try {
const file = await readFile(path, 'utf8');
let fileContent;

if (file) {
fileContent = yaml.load(file);
}

if (typeof fileContent !== 'object') {
throw new yaml.YAMLException(`${path} is not a valid yaml`);
}

if (fileContent) {
const {
scenario,
scenarios,
Expand All @@ -31,7 +47,8 @@ const parseConfigFile = async (path) => {
ignoreHTTPSErrors,
locale,
timezoneId,
} = yaml.load(file);
} = fileContent;

return {
args: {
scenarios,
Expand Down Expand Up @@ -60,8 +77,12 @@ const parseConfigFile = async (path) => {
},
};
}
} catch {
// Do Nothing
} catch (error) {
if (error.name === 'YAMLException') {
throw new yaml.YAMLException(`${path} is not a valid yaml`);
} else if (!isMissingDefaultConfigFile(path, error)) {
throw error;
}
}
};

Expand Down

0 comments on commit 66a3f3a

Please sign in to comment.