Skip to content

Commit

Permalink
👷 Add an "invalid" test in the CI
Browse files Browse the repository at this point in the history
  • Loading branch information
ouvreboite committed Jun 12, 2024
1 parent e1cc87c commit 6c98bad
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 22 deletions.
27 changes: 24 additions & 3 deletions .github/workflows/poltergust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ main ]

jobs:
test:
build-and-test-valid-example:
runs-on: ubuntu-latest

steps:
Expand All @@ -20,5 +20,26 @@ jobs:
- name: Install poltergust
run: npm ci

- name: Run Poltergust tests
run: npx poltergust test ./example
- name: Test the example ruleset
run: npx poltergust test ./example

build-and-test-invalid-example:
needs: build-and-test-valid-example
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Use Node.js
uses: actions/setup-node@v2

- name: Install poltergust
run: npm ci

- name: Test the example ruleset
run: npx poltergust test ./example

- name: Test the invalid example ruleset (should exit 1)
run: npx poltergust test ./invalid-example || (exitcode=$?; [ $exitcode -eq 1 ] || exit $exitcode)
shell: bash
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Run Poltergust Tests](https://github.com/ouvreboite/api-guidelines/actions/workflows/poltergust.yaml/badge.svg)](https://github.com/ouvreboite/api-guidelines/actions/workflows/poltergust.yaml)

# 👻 poltergust

An npm CLI to extract Spectral rules from .md files, merge them and test them.
Expand Down
2 changes: 1 addition & 1 deletion example/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ paths:
/mouses/{mice-id}:
get:
parameters:
- name: mice-id #spectral-should-fail-here-: path-parameters-must-be-kebab-case
- name: mice-id #spectral-should-not-fail-here-: path-parameters-must-be-kebab-case
in: path
required: true
schema:
Expand Down
35 changes: 19 additions & 16 deletions functions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,28 @@ export function generateRuleFileContent(spectralRulesContents, rulesDir){
* @return {boolean} success
*/
export async function runTestCase(rule, testCase, rulesDir){
//write rule file to temp dir
const tempDir = await mkdtemp(rule.name+"-test-");
const tempRuleFilePath = path.join(tempDir, '.spectral.yaml');
const functionsDirPath = path.join(rulesDir, 'functions');
const tempFunctionsDirPath = path.join(tempDir, 'functions')
let ruleFile = generateRuleFileContent([rule.content], rulesDir);
fs.writeFileSync(tempRuleFilePath, ruleFile);

//copy the functions folder into the temp dir
fs.mkdirSync(tempFunctionsDirPath);
fs.readdirSync(functionsDirPath).forEach(file => {
const sourceFile = path.join(functionsDirPath, file);
const destFile = path.join(tempFunctionsDirPath, file);
fs.copyFileSync(sourceFile, destFile);
});

let ok = true;

//write rule file to temp dir
const tempDir = await mkdtemp(rule.name+"-test-");
try{
const tempRuleFilePath = path.join(tempDir, '.spectral.yaml');
let ruleFile = generateRuleFileContent([rule.content], rulesDir);
fs.writeFileSync(tempRuleFilePath, ruleFile);

//if the function dir exists, copy the functions folder into the temp dir
const functionsDirPath = path.join(rulesDir, 'functions');
if(fs.existsSync(functionsDirPath)){
const tempFunctionsDirPath = path.join(tempDir, 'functions')
fs.mkdirSync(tempFunctionsDirPath);

fs.readdirSync(functionsDirPath).forEach(file => {
const sourceFile = path.join(functionsDirPath, file);
const destFile = path.join(tempFunctionsDirPath, file);
fs.copyFileSync(sourceFile, destFile);
});
}

//run test case against the rule
const spectral = new Spectral();
spectral.setRuleset(await bundleAndLoadRuleset(path.resolve(tempRuleFilePath), { fs, fetch }));
Expand Down
5 changes: 3 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ if (args[0] === 'merge') {
}

for(let testCase of testCases){
await runTestCase(rule, testCase, rulesDir);
const testCaseSuccess = await runTestCase(rule, testCase, rulesDir);
if(! testCaseSuccess)
process.exitCode = 1;
}
}
process.exit(1);
}
35 changes: 35 additions & 0 deletions invalid-example/invalid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# API rules

1. [base-path-must-start-with-slash](#base-path-must-start-with-slash)

## base-path-must-start-with-slash

```yaml
#spectral-test
#spectral-should-not-fail-anywhere-✅: base-path-must-start-with-slash
openapi: 3.0.1
info:
title: Test
version: 1.0.0
servers:
- url: do-not-start-with-slash #in fact, it should fail here
```
<details>
<summary>Spectral rule 🤖</summary>
```yaml
#spectral-rule
base-path-must-start-with-slash:
description: Base path must start with /.
message: "{{description}}. But was {{value}}."
given: $.servers[*]
severity: error
then:
field: url
function: pattern
functionOptions:
match: "^\/"
```
</details>
8 changes: 8 additions & 0 deletions invalid-example/spectral.base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
formats: ["oas3"]
extends: "spectral:oas"
aliases:
parameters:
- $.paths[*].parameters
- $.paths[*][*].parameters
#rules will be injected from the rules/*.md files
rules:
19 changes: 19 additions & 0 deletions invalid-example/spectral.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
formats: ["oas3"]
extends: "spectral:oas"
aliases:
parameters:
- $.paths[*].parameters
- $.paths[*][*].parameters
#rules will be injected from the rules/*.md files
rules:
base-path-must-start-with-slash:
description: Base path must start with /.
message: "{{description}}. But was {{value}}."
given: $.servers[*]
severity: error
then:
field: url
function: pattern
functionOptions:
match: "^\/"

0 comments on commit 6c98bad

Please sign in to comment.