-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
[TypeSpecValidation] Add tests for LinterRuleset #29267
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
49567ae
Remove dead code
mikeharder 1722e1a
Refactor to simplify
mikeharder 140240a
Add basic success test
mikeharder 34da4bb
Read config from host
mikeharder 33deaae
Revert "Refactor to simplify"
mikeharder 499c596
Revert "Remove dead code"
mikeharder 0aa1ac9
Handle empty config
mikeharder a86ec4d
Add tests for existing functionality
mikeharder 3682cf1
Remove unused import
mikeharder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { readFile } from "fs/promises"; | ||
import { join } from "path"; | ||
import { parse as yamlParse } from "yaml"; | ||
import { Rule } from "../rule.js"; | ||
|
@@ -16,12 +15,11 @@ export class LinterRulesetRule implements Rule { | |
let stdOutput = ""; | ||
let errorOutput = ""; | ||
|
||
const configFile = join(folder, "tspconfig.yaml"); | ||
const configText = await readFile(configFile, "utf8"); | ||
const configText = await host.readTspConfig(folder); | ||
const config = yamlParse(configText); | ||
|
||
const rpFolder = | ||
config.options?.["@azure-tools/typespec-autorest"]?.["azure-resource-provider-folder"]; | ||
config?.options?.["@azure-tools/typespec-autorest"]?.["azure-resource-provider-folder"]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle empty config. Makes this rule consistent with others. |
||
stdOutput += `azure-resource-provider-folder: ${JSON.stringify(rpFolder)}\n`; | ||
|
||
const mainTspExists = await host.checkFileExists(join(folder, "main.tsp")); | ||
|
@@ -35,7 +33,7 @@ export class LinterRulesetRule implements Rule { | |
} | ||
stdOutput += `files: ${JSON.stringify(files)}\n`; | ||
|
||
const linterExtends = config.linter?.extends; | ||
const linterExtends = config?.linter?.extends; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle empty config. Makes this rule consistent with others. |
||
stdOutput += `linter.extends: ${JSON.stringify(linterExtends)}`; | ||
|
||
let requiredRuleset = ""; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { describe, it } from "vitest"; | ||
import { join } from "path"; | ||
import { LinterRulesetRule } from "../src/rules/linter-ruleset.js"; | ||
import { TsvTestHost } from "./tsv-test-host.js"; | ||
import { strict as assert } from "node:assert"; | ||
|
||
describe("linter-ruleset", function () { | ||
it("succeeds with default config", async function () { | ||
const host = new TsvTestHost(); | ||
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); | ||
assert(result.success); | ||
}); | ||
|
||
it("succeeds with resource-manager/resource-manager", async function () { | ||
const host = new TsvTestHost(); | ||
host.readTspConfig = async (_folder: string) => ` | ||
options: | ||
"@azure-tools/typespec-autorest": | ||
azure-resource-provider-folder: "resource-manager" | ||
linter: | ||
extends: | ||
- "@azure-tools/typespec-azure-rulesets/resource-manager" | ||
`; | ||
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); | ||
assert(result.success); | ||
}); | ||
|
||
it("succeeds with data-plane/data-plane", async function () { | ||
const host = new TsvTestHost(); | ||
host.readTspConfig = async (_folder: string) => ` | ||
options: | ||
"@azure-tools/typespec-autorest": | ||
azure-resource-provider-folder: "data-plane" | ||
linter: | ||
extends: | ||
- "@azure-tools/typespec-azure-rulesets/data-plane" | ||
`; | ||
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); | ||
assert(result.success); | ||
}); | ||
|
||
it("succeeds with client.tsp/data-plane", async function () { | ||
const host = new TsvTestHost(); | ||
host.checkFileExists = async (file: string) => file === join(TsvTestHost.folder, "client.tsp"); | ||
host.readTspConfig = async (_folder: string) => ` | ||
linter: | ||
extends: | ||
- "@azure-tools/typespec-azure-rulesets/data-plane" | ||
`; | ||
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); | ||
assert(result.success); | ||
}); | ||
|
||
it("fails with no-config", async function () { | ||
const host = new TsvTestHost(); | ||
host.readTspConfig = async (_folder: string) => ""; | ||
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); | ||
assert(!result.success); | ||
}); | ||
|
||
it("fails with resource-manager/no-linter", async function () { | ||
const host = new TsvTestHost(); | ||
host.readTspConfig = async (_folder: string) => ` | ||
options: | ||
"@azure-tools/typespec-autorest": | ||
azure-resource-provider-folder: "resource-manager" | ||
`; | ||
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); | ||
assert(!result.success); | ||
}); | ||
|
||
it("fails with resource-manager/data-plane", async function () { | ||
const host = new TsvTestHost(); | ||
host.readTspConfig = async (_folder: string) => ` | ||
options: | ||
"@azure-tools/typespec-autorest": | ||
azure-resource-provider-folder: "resource-manager" | ||
linter: | ||
extends: | ||
- "@azure-tools/typespec-azure-rulesets/data-plane" | ||
`; | ||
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); | ||
assert(!result.success); | ||
}); | ||
|
||
it("fails with data-plane/resource-manager", async function () { | ||
const host = new TsvTestHost(); | ||
host.readTspConfig = async (_folder: string) => ` | ||
options: | ||
"@azure-tools/typespec-autorest": | ||
azure-resource-provider-folder: "data-plane" | ||
linter: | ||
extends: | ||
- "@azure-tools/typespec-azure-rulesets/resource-manager" | ||
`; | ||
const result = await new LinterRulesetRule().execute(host, TsvTestHost.folder); | ||
assert(!result.success); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rules should only read config via
host
. Makes this rule consistent with others.