Skip to content

Commit

Permalink
fix(ruleset-migrator): http/https uris not followed correctly (#2247)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Aug 30, 2022
1 parent 7d16080 commit 573e112
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
55 changes: 55 additions & 0 deletions packages/ruleset-migrator/src/__tests__/ruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,61 @@ describe('migrator', () => {
});
});

it('should follow links correctly', async () => {
serveAssets({
'http://domain/bitbucket/projects/API/repos/spectral-rules/raw/.spectral.yml?at=refs%2Fheads%2Fmaster': {
extends: ['spectral:oas', 'oas-rules.yml'],
rules: {
'valid-type': 'error',
},
},
'http://domain/bitbucket/projects/API/repos/spectral-rules/raw/oas-rules.yml': {
rules: {
'valid-type': {
given: '$',
function: {
then: 'truthy',
},
},
},
},
});

await vol.promises.writeFile(
path.join(cwd, 'ruleset.json'),
JSON.stringify({
extends: [
'http://domain/bitbucket/projects/API/repos/spectral-rules/raw/.spectral.yml?at=refs%2Fheads%2Fmaster',
],
}),
);

expect(
await migrateRuleset(path.join(cwd, 'ruleset.json'), {
format: 'esm',
fs: vol as any,
}),
).toEqual(`import {oas} from "@stoplight/spectral-rulesets";
export default {
"extends": [{
"extends": [oas, {
"rules": {
"valid-type": {
"given": "$",
"function": {
"then": "truthy"
}
}
}
}],
"rules": {
"valid-type": "error"
}
}]
};
`);
});

describe('custom npm registry', () => {
it('should be supported', async () => {
serveAssets({
Expand Down
10 changes: 5 additions & 5 deletions packages/ruleset-migrator/src/tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ export class Tree {
if (path.isURL(identifier) || path.isAbsolute(identifier)) {
resolved = identifier;
this.#resolvedPaths.add(identifier);
} else if (kind === 'ruleset' && isPackageImport(identifier)) {
resolved =
ctx.npmRegistry !== null
? path.join(ctx.npmRegistry, identifier)
: requireResolve?.(identifier, { paths: [ctx.cwd] }) ?? path.join(ctx.cwd, identifier);
} else if (
(ctx.npmRegistry !== null && ctx.filepath.startsWith(ctx.npmRegistry)) ||
isKnownNpmRegistry(ctx.filepath)
Expand All @@ -142,6 +137,11 @@ export class Tree {
// <origin>/<pkg-name>
// <origin>/<pkg-name>/<asset> where asset can be a custom fn, etc.
resolved = path.join(ctx.filepath, identifier);
} else if (kind === 'ruleset' && !path.isURL(ctx.filepath) && isPackageImport(identifier)) {
resolved =
ctx.npmRegistry !== null
? path.join(ctx.npmRegistry, identifier)
: requireResolve?.(identifier, { paths: [ctx.cwd] }) ?? path.join(ctx.cwd, identifier);
} else {
resolved = path.join(ctx.filepath, '..', identifier);
this.#resolvedPaths.add(resolved);
Expand Down

0 comments on commit 573e112

Please sign in to comment.