Skip to content

Commit

Permalink
feat(): add tests for checkForConsoleUsage and checkForPresenceTests;…
Browse files Browse the repository at this point in the history
… refactor path usage in checks; add new utility functions; rename some existing functions; add test data; update README with some roadmap items
  • Loading branch information
mikaelvesavuori committed Jul 8, 2023
1 parent 2e1b812 commit f45866f
Show file tree
Hide file tree
Showing 40 changed files with 1,037 additions and 3,752 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Files
.env
.dccache
.DS_Store
standardlint.json
standardlint.results.json

Expand All @@ -16,4 +17,4 @@ jest-coverage/

# Diagrams
**/*.bkp
**/*.bak
**/*.bak
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,12 @@ Checks if there is a template for GitHub Pull Requests.

Checks:

- Service metadata: Do you link to observability resources (logs/metrics/traces/dashboards etc.)?
- Did you update docs (Markdown)?
- Did you update API schema (YAML/JSON)?
- More ArchUnit/fitness functions kind of support?
- Ensure console.log() is not used
- Ensure imports following acceptable conventions
- No cyclic methods/dependencies
- Ensure there are tests
- Documentation coverage
- Ensure methods/functions are below a certain threshold in terms of lines of code
- Support for external config

Tech:

- CLI?
- Do:
- Documentation coverage
- Ensure imports follow acceptable conventions
- No cyclic methods/dependencies
- Ensure methods/functions are below a certain threshold in terms of lines of code
- Check for throwing native errors
- Do later:
- Service metadata: Do you link to observability resources (logs/metrics/traces/dashboards etc.)?
- Support for external config
4,345 changes: 726 additions & 3,619 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "standardlint",
"description": "Extensible standards linter and auditor.",
"version": "1.0.3",
"version": "1.0.4",
"author": "Mikael Vesavuori",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -47,13 +47,13 @@
"prepare": "husky install"
},
"devDependencies": {
"@ava/typescript": "3",
"@ava/typescript": "4",
"@types/node": "latest",
"@typescript-eslint/eslint-plugin": "5",
"@typescript-eslint/parser": "5",
"@vercel/ncc": "0",
"ava": "5",
"c8": "7",
"c8": "8",
"eslint": "8",
"eslint-config-prettier": "8",
"eslint-plugin-prettier": "4",
Expand All @@ -63,7 +63,7 @@
"ts-node": "latest",
"tslib": "latest",
"type-coverage": "2",
"typescript": "4"
"typescript": "5"
},
"ava": {
"typescript": {
Expand Down
6 changes: 3 additions & 3 deletions src/checks/checkForConflictingLockfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CheckResult, Severity } from '../interface/Check';

import { calculatePass } from '../application/calculatePass';

import { checkIfFileOrDirectoryExists } from '../utils/checkIfFileOrDirectoryExists';
import { exists } from '../utils/exists';

/**
* @description Checks if there are conflicting Node package lock files.
Expand All @@ -11,8 +11,8 @@ export function checkForConflictingLockfiles(severity: Severity, basePath: strin
const name = 'Lock files';
const message = 'Check for conflicting lock files';

const npmLockfile = checkIfFileOrDirectoryExists(basePath, 'package-lock.json');
const yarnLockfile = checkIfFileOrDirectoryExists(basePath, 'yarn.lock');
const npmLockfile = exists(basePath, 'package-lock.json');
const yarnLockfile = exists(basePath, 'yarn.lock');

const result = !(npmLockfile && yarnLockfile);

Expand Down
34 changes: 34 additions & 0 deletions src/checks/checkForConsoleUsage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CheckResult, Severity } from '../interface/Check';

import { calculatePass } from '../application/calculatePass';

import { logDefaultPathMessage } from '../utils/logDefaultPathMessage';
import { getAllFiles } from '../utils/getAllFiles';
import { readFile } from '../utils/readFile';

/**
* @description Checks if there is an API schema.
*/
export function checkForConsoleUsage(
severity: Severity,
basePath: string,
customPath?: string
): CheckResult {
const path = customPath || 'src';
const name = 'Console usage';
const message = 'Check for console usage';

if (!customPath) logDefaultPathMessage(name, path);

const tests = getAllFiles(`${basePath}/${path}`, []);
const regex = /console.(.*)/gi;
const includesConsole = tests.map((test: string) => regex.test(readFile(test)));
const result = !includesConsole.some(() => true); // We don't want any occurrences

return {
name,
status: calculatePass(result, severity),
message,
path
};
}
11 changes: 4 additions & 7 deletions src/checks/checkForDefinedRelations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ export function checkForDefinedRelations(
basePath: string,
customPath?: string
): CheckResult {
const SERVICE_METADATA_FILE_PATH = customPath || 'manifest.json';
const path = customPath || 'manifest.json';
const name = 'Relations';
const message = 'Check for defined relations';

if (!customPath) logDefaultPathMessage(name, SERVICE_METADATA_FILE_PATH);
if (!customPath) logDefaultPathMessage(name, path);

const serviceMetadata: Record<string, any> = getJSONFileContents(
basePath,
SERVICE_METADATA_FILE_PATH
);
const serviceMetadata: Record<string, any> = getJSONFileContents(basePath, path);

const result =
serviceMetadata && serviceMetadata?.relations && serviceMetadata?.relations.length > 0;
Expand All @@ -31,6 +28,6 @@ export function checkForDefinedRelations(
name,
status: calculatePass(result, severity),
message,
path: SERVICE_METADATA_FILE_PATH
path
};
}
11 changes: 4 additions & 7 deletions src/checks/checkForDefinedServiceLevelObjectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@ export function checkForDefinedServiceLevelObjectives(
basePath: string,
customPath?: string
): CheckResult {
const SERVICE_METADATA_FILE_PATH = customPath || 'manifest.json';
const path = customPath || 'manifest.json';
const name = 'SLOs';
const message = 'Check for defined Service Level Objectives';

if (!customPath) logDefaultPathMessage(name, SERVICE_METADATA_FILE_PATH);
if (!customPath) logDefaultPathMessage(name, path);

const serviceMetadata: Record<string, any> = getJSONFileContents(
basePath,
SERVICE_METADATA_FILE_PATH
);
const serviceMetadata: Record<string, any> = getJSONFileContents(basePath, path);

const result = serviceMetadata && serviceMetadata?.slo && serviceMetadata?.slo.length > 0;

return {
name,
status: calculatePass(result, severity),
message,
path: SERVICE_METADATA_FILE_PATH
path
};
}
11 changes: 4 additions & 7 deletions src/checks/checkForDefinedTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ export function checkForDefinedTags(
basePath: string,
customPath?: string
): CheckResult {
const SERVICE_METADATA_FILE_PATH = customPath || 'manifest.json';
const path = customPath || 'manifest.json';
const name = 'Tags';
const message = 'Check for defined tags';

if (!customPath) logDefaultPathMessage(name, SERVICE_METADATA_FILE_PATH);
if (!customPath) logDefaultPathMessage(name, path);

const serviceMetadata: Record<string, any> = getJSONFileContents(
basePath,
SERVICE_METADATA_FILE_PATH
);
const serviceMetadata: Record<string, any> = getJSONFileContents(basePath, path);

const result =
serviceMetadata && serviceMetadata?.spec?.tags && serviceMetadata?.spec?.tags.length > 0;
Expand All @@ -31,6 +28,6 @@ export function checkForDefinedTags(
name,
status: calculatePass(result, severity),
message,
path: SERVICE_METADATA_FILE_PATH
path
};
}
1 change: 1 addition & 0 deletions src/checks/checkForDocumentationCoverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const regex = /\/\*\*\s*\n([^\*]|\*[^\/])*\*\//;
10 changes: 5 additions & 5 deletions src/checks/checkForPresenceApiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CheckResult, Severity } from '../interface/Check';

import { calculatePass } from '../application/calculatePass';

import { checkIfFileOrDirectoryExists } from '../utils/checkIfFileOrDirectoryExists';
import { exists } from '../utils/exists';
import { logDefaultPathMessage } from '../utils/logDefaultPathMessage';

/**
Expand All @@ -13,18 +13,18 @@ export function checkForPresenceApiSchema(
basePath: string,
customPath?: string
): CheckResult {
const API_SCHEMA_PATH = customPath || 'api/schema.json';
const path = customPath || 'api/schema.json';
const name = 'API schema';
const message = 'Check for API schema';

if (!customPath) logDefaultPathMessage(name, API_SCHEMA_PATH);
if (!customPath) logDefaultPathMessage(name, path);

const result = checkIfFileOrDirectoryExists(basePath, API_SCHEMA_PATH);
const result = exists(basePath, path);

return {
name,
status: calculatePass(result, severity),
message,
path: API_SCHEMA_PATH
path
};
}
8 changes: 4 additions & 4 deletions src/checks/checkForPresenceChangelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { CheckResult, Severity } from '../interface/Check';

import { calculatePass } from '../application/calculatePass';

import { checkIfFileOrDirectoryExists } from '../utils/checkIfFileOrDirectoryExists';
import { exists } from '../utils/exists';

/**
* @description Checks if there is a `CHANGELOG.md` file.
*/
export function checkForPresenceChangelog(severity: Severity, basePath: string): CheckResult {
const CHANGELOG_FILE_PATH = 'CHANGELOG.md';
const path = 'CHANGELOG.md';
const name = 'Changelog';
const message = 'Check for CHANGELOG file';

const result = checkIfFileOrDirectoryExists(basePath, CHANGELOG_FILE_PATH);
const result = exists(basePath, path);

return {
name,
status: calculatePass(result, severity),
message,
path: CHANGELOG_FILE_PATH
path
};
}
10 changes: 5 additions & 5 deletions src/checks/checkForPresenceCiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CheckResult, Severity } from '../interface/Check';

import { calculatePass } from '../application/calculatePass';

import { checkIfFileOrDirectoryExists } from '../utils/checkIfFileOrDirectoryExists';
import { exists } from '../utils/exists';
import { logDefaultPathMessage } from '../utils/logDefaultPathMessage';

/**
Expand All @@ -13,18 +13,18 @@ export function checkForPresenceCiConfig(
basePath: string,
customPath?: string
): CheckResult {
const CONFIG_PATH = customPath || '.github/workflows/main.yml';
const path = customPath || '.github/workflows/main.yml';
const name = 'CI configuration';
const message = 'Check for CI configuration file';

if (!customPath) logDefaultPathMessage(name, CONFIG_PATH);
if (!customPath) logDefaultPathMessage(name, path);

const result = checkIfFileOrDirectoryExists(basePath, CONFIG_PATH);
const result = exists(basePath, path);

return {
name,
status: calculatePass(result, severity),
message,
path: CONFIG_PATH
path
};
}
8 changes: 4 additions & 4 deletions src/checks/checkForPresenceCodeowners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { CheckResult, Severity } from '../interface/Check';

import { calculatePass } from '../application/calculatePass';

import { checkIfFileOrDirectoryExists } from '../utils/checkIfFileOrDirectoryExists';
import { exists } from '../utils/exists';

/**
* @description Checks if there is a `CODEOWNERS` file.
*/
export function checkForPresenceCodeowners(severity: Severity, basePath: string): CheckResult {
const CODEOWNERS_FILE_PATH = 'CODEOWNERS';
const path = 'CODEOWNERS';
const name = 'Code owners';
const message = 'Check for CODEOWNERS file';

const result = checkIfFileOrDirectoryExists(basePath, CODEOWNERS_FILE_PATH);
const result = exists(basePath, path);

return {
name,
status: calculatePass(result, severity),
message,
path: CODEOWNERS_FILE_PATH
path
};
}
8 changes: 4 additions & 4 deletions src/checks/checkForPresenceContributing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { CheckResult, Severity } from '../interface/Check';

import { calculatePass } from '../application/calculatePass';

import { checkIfFileOrDirectoryExists } from '../utils/checkIfFileOrDirectoryExists';
import { exists } from '../utils/exists';

/**
* @description Checks if there is a `CONTRIBUTING.md` file.
*/
export function checkForPresenceContributing(severity: Severity, basePath: string): CheckResult {
const CONTRIBUTING_FILE_PATH = 'CONTRIBUTING.md';
const path = 'CONTRIBUTING.md';
const name = 'Contribution information';
const message = 'Check for CONTRIBUTING file';

const result = checkIfFileOrDirectoryExists(basePath, CONTRIBUTING_FILE_PATH);
const result = exists(basePath, path);

return {
name,
status: calculatePass(result, severity),
message,
path: CONTRIBUTING_FILE_PATH
path
};
}
Loading

0 comments on commit f45866f

Please sign in to comment.