diff --git a/crates/ide-host/src/ops/bdd.rs b/crates/ide-host/src/ops/bdd.rs index e8043abd0..ae3565dc6 100644 --- a/crates/ide-host/src/ops/bdd.rs +++ b/crates/ide-host/src/ops/bdd.rs @@ -1,6 +1,7 @@ //! Cucumber sources: the project's `.feature` files and the JavaScript / TypeScript files //! (`@cucumber/cucumber`, `playwright-bdd`) and Java / Kotlin files (cucumber-jvm's -//! `@Given("…")` annotations and Java 8 lambdas) that could define steps, with their text. The UI +//! `@Given("…")` annotations and Java 8 lambdas) and Python files (behave and pytest-bdd's +//! `@given('…')` decorators) that could define steps, with their text. The UI //! owns the Gherkin and step-expression parsing and the index it feeds (as for `routes`), //! so this side stays a cheap scan. @@ -21,6 +22,8 @@ const MAX_FILE_BYTES: u64 = 2 << 20; /// What a step definition call starts with (`Given('…'`, `defineStep(/…/`). const STEP_CALLS: [&str; 5] = ["Given(", "When(", "Then(", "Step(", "defineStep("]; +/** What a Python step decorator starts with (behave also takes the capitalized names). */ +const PYTHON_STEP_CALLS: [&str; 8] = ["@given(", "@when(", "@then(", "@step(", "@Given(", "@When(", "@Then(", "@Step("]; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)] #[serde(rename_all = "camelCase")] @@ -38,10 +41,14 @@ fn is_feature(path: &Path) -> bool { fn is_script(path: &Path) -> bool { matches!( path.extension().and_then(|e| e.to_str()), - Some("js" | "jsx" | "mjs" | "cjs" | "ts" | "tsx" | "mts" | "cts" | "java" | "kt") + Some("js" | "jsx" | "mjs" | "cjs" | "ts" | "tsx" | "mts" | "cts" | "java" | "kt" | "py") ) } +fn is_python(path: &Path) -> bool { + path.extension().and_then(|e| e.to_str()) == Some("py") +} + /// The feature files and step definition files under `root`. pub(crate) fn sources_in(root: &Path, excludes: &[String]) -> Vec { let found: Mutex> = Mutex::new(Vec::new()); @@ -55,7 +62,8 @@ pub(crate) fn sources_in(root: &Path, excludes: &[String]) -> Vec { return; } let Ok(text) = std::fs::read_to_string(path) else { return }; - if !feature && !STEP_CALLS.iter().any(|call| text.contains(call)) { + let calls: &[&str] = if is_python(path) { &PYTHON_STEP_CALLS } else { &STEP_CALLS }; + if !feature && !calls.iter().any(|call| text.contains(call)) { return; } let Ok(mut found) = found.lock() else { return }; @@ -76,6 +84,7 @@ pub fn cucumber_sources(host: &Host, window: &str) -> IdeResult> let mut excludes = project.search_excludes(); // Cucumber's own sources (and every other package's) are not the project's steps. excludes.push("node_modules".to_owned()); + excludes.push("site-packages".to_owned()); let mut out = Vec::new(); for root in std::iter::once(project.root.clone()).chain(project.content_roots()) { out.extend(sources_in(&root, &excludes)); @@ -104,11 +113,23 @@ mod tests { std::fs::write(root.join("src/CartSteps.java"), "@Given(\"an empty cart\")\npublic void empty() {}").unwrap(); std::fs::write(root.join("src/App.java"), "class App {}").unwrap(); std::fs::write(root.join("src/notes.md"), "Given('x')").unwrap(); + std::fs::write(root.join("steps/cart_steps.py"), "@given('an empty cart')\ndef step_impl(context): pass") + .unwrap(); + std::fs::write(root.join("src/util.py"), "def given(x): return x\nWhen = given('x')").unwrap(); std::fs::write(root.join("node_modules/@cucumber/cucumber/lib/index.js"), "Given('x')").unwrap(); let files = sources_in(root, &["node_modules".to_owned()]); let names: Vec = files.iter().map(|f| f.path.0.strip_prefix(root).unwrap().to_string_lossy().replace('\\', "/")).collect(); - assert_eq!(names, ["features/cart.feature", "src/CartSteps.java", "steps/bdd.mjs", "steps/cart.steps.ts"]); + assert_eq!( + names, + [ + "features/cart.feature", + "src/CartSteps.java", + "steps/bdd.mjs", + "steps/cart.steps.ts", + "steps/cart_steps.py" + ] + ); assert!(files[0].feature && !files[1].feature); assert!(files[3].text.contains("an empty cart")); } diff --git a/src/features/cucumber/index.ts b/src/features/cucumber/index.ts index 71eb7f99e..50f54111e 100644 --- a/src/features/cucumber/index.ts +++ b/src/features/cucumber/index.ts @@ -42,7 +42,7 @@ import { usagesOf, } from "./stepIndex"; -const SCRIPT = /\.(?:[cm]?[jt]sx?|java|kt)$/i; +const SCRIPT = /\.(?:[cm]?[jt]sx?|java|kt|py)$/i; const FEATURE = /\.feature$/i; const DELAY_MS = 300; @@ -168,7 +168,7 @@ export function activate(): () => void { return targets.length ? { from: step.from, to: step.to, targets } : null; } if (!SCRIPT.test(doc.path)) return null; - const definition = stepDefinitions(text).find((d) => d.from <= offset && offset < d.to); + const definition = stepDefinitions(text, doc.path).find((d) => d.from <= offset && offset < d.to); if (!definition) return null; const targets = await usagesOf(definition); return targets.length ? { from: definition.from, to: definition.to, targets } : null; diff --git a/src/features/cucumber/pythonSteps.test.ts b/src/features/cucumber/pythonSteps.test.ts new file mode 100644 index 000000000..cfba58ee7 --- /dev/null +++ b/src/features/cucumber/pythonSteps.test.ts @@ -0,0 +1,123 @@ +import { describe, expect, it } from "vitest"; +import { definitionInsertions, matchesStep, snippetStyle, stepDefinitions, stepSnippet, type FeatureStep } from "./steps"; + +const step = (text: string): FeatureStep => ({ from: 0, to: 0, keyword: "Given ", type: "Given", text, matchText: text }); + +const apply = (text: string, insertions: { at: number; text: string }[]) => + [...insertions].sort((a, b) => b.at - a.at).reduce((out, i) => out.slice(0, i.at) + i.text + out.slice(i.at), text); + +describe("behave steps", () => { + const BEHAVE = `from behave import given, then + +@given('an empty cart') +def step_impl(context): + pass + +@given(u"a cart with {count:d} items") +def step_impl(context, count): + pass + +@then('the total is {amount:f} {currency}') +def step_impl(context, amount, currency): + pass + +use_step_matcher("re") + +@when(r'^I remove (?P\\d+) items?$') +def step_impl(context, count): + pass + +@step("""I check out""") +def step_impl(context): + pass +`; + + it("reads parse formats, and regular expressions after use_step_matcher", () => { + const defs = stepDefinitions(BEHAVE, "features/steps/cart_steps.py"); + expect(defs.map((d) => d.source)).toEqual([ + "an empty cart", + "a cart with {count:d} items", + "the total is {amount:f} {currency}", + "^I remove (?P\\d+) items?$", + "I check out", + ]); + const [empty, count, total, remove, checkout] = defs; + expect(BEHAVE.slice(count!.from, count!.to)).toBe('u"a cart with {count:d} items"'); + expect(matchesStep(empty!, step("an empty cart"))).toBe(true); + expect(matchesStep(empty!, step("an empty cart now"))).toBe(false); + expect(matchesStep(count!, step("a cart with 3 items"))).toBe(true); + expect(matchesStep(count!, step("a cart with three items"))).toBe(false); + expect(matchesStep(total!, step("the total is 3.50 EUR"))).toBe(true); + expect(matchesStep(remove!, step("I remove 2 items"))).toBe(true); + expect(matchesStep(remove!, step("I remove all items"))).toBe(false); + // `use_step_matcher("re")` stays in effect: a plain string is a regular expression. + expect(matchesStep(checkout!, step("I check out"))).toBe(true); + }); + + it("writes a pending step_impl and imports the decorator", () => { + expect(snippetStyle("features/steps/cart_steps.py", BEHAVE)).toBe("behave"); + const snippet = stepSnippet("When", "I pay {gift} 'now'", "behave"); + expect(snippet.text).toBe( + [ + "@when('I pay {{gift}} \\'now\\'')", + "def step_impl(context):", + " raise NotImplementedError('STEP: When I pay {gift} \\'now\\'')", + "", + ].join("\n"), + ); + const { insertions, select } = definitionInsertions(BEHAVE, "When", snippet, "behave"); + const out = apply(BEHAVE, insertions); + expect(out.startsWith("from behave import given, then, when\n")).toBe(true); + expect(out.endsWith(` pass\n\n\n${snippet.text}`)).toBe(true); + expect(out.split("\n")[select.line]!.slice(select.from, select.to)).toMatch(/^raise NotImplementedError/); + const created = stepDefinitions(out, "steps.py").find((d) => d.source.startsWith("I pay"))!; + expect(created).toBeDefined(); + }); + + it("matches a created step's braces literally", () => { + const text = `from behave import when\n\n${stepSnippet("When", "I pay {gift}", "behave").text}`; + const [created] = stepDefinitions(text, "steps.py"); + expect(matchesStep(created!, step("I pay {gift}"))).toBe(true); + }); +}); + +describe("pytest-bdd steps", () => { + const PYTEST_BDD = `from pytest_bdd import given, parsers, scenarios, when + +scenarios("cart.feature") + +@given("there are {count} cucumbers") +def literal(): + pass + +@given(parsers.parse("I have {start:d} cucumbers"), target_fixture="cucumbers") +def start(start): + return start + +@when(parsers.re(r"I eat (?P\\d+) cucumbers")) +def eat(cucumbers, eat): + pass +`; + + it("matches plain strings exactly and parsers by their kind", () => { + const [literal, start, eat] = stepDefinitions(PYTEST_BDD, "tests/test_cart.py"); + expect(matchesStep(literal!, step("there are {count} cucumbers"))).toBe(true); + expect(matchesStep(literal!, step("there are 5 cucumbers"))).toBe(false); + expect(matchesStep(start!, step("I have 12 cucumbers"))).toBe(true); + expect(matchesStep(eat!, step("I eat 5 cucumbers"))).toBe(true); + expect(matchesStep(eat!, step("I eat 5 cucumbers today"))).toBe(false); + }); + + it("writes an anonymous pending step", () => { + expect(snippetStyle("tests/test_cart.py", PYTEST_BDD)).toBe("pytest-bdd"); + const snippet = stepSnippet("Then", "I have {count} cucumbers", "pytest-bdd"); + expect(snippet.text).toBe( + ["@then('I have {count} cucumbers')", "def _():", " raise NotImplementedError", ""].join("\n"), + ); + const { insertions } = definitionInsertions(PYTEST_BDD, "Then", snippet, "pytest-bdd"); + const out = apply(PYTEST_BDD, insertions); + expect(out.startsWith("from pytest_bdd import given, parsers, scenarios, when, then\n")).toBe(true); + const created = stepDefinitions(out, "tests/test_cart.py").at(-1)!; + expect(matchesStep(created, step("I have {count} cucumbers"))).toBe(true); + }); +}); diff --git a/src/features/cucumber/pythonSteps.ts b/src/features/cucumber/pythonSteps.ts new file mode 100644 index 000000000..aabc24cf0 --- /dev/null +++ b/src/features/cucumber/pythonSteps.ts @@ -0,0 +1,171 @@ +/** + * Python step definitions: behave's `@given('…')` decorators (with its `parse`, `cfparse` and + * `re` step matchers, switched by `use_step_matcher`) and pytest-bdd's (plain strings matched + * exactly, `parsers.parse` / `cfparse` / `re`). + */ +import type { StepDefinition } from "./steps"; + +export type PythonFramework = "behave" | "pytest-bdd"; + +type Matcher = "parse" | "re" | "string"; + +/** pytest-bdd when the file imports it; behave otherwise. */ +export function pythonFramework(text: string): PythonFramework { + return /^[ \t]*(?:from|import)[ \t]+pytest_bdd\b/m.test(text) ? "pytest-bdd" : "behave"; +} + +const DECORATOR = /@(?:given|when|then|step|Given|When|Then|Step)\s*\(\s*/g; +const STEP_MATCHER = /\b(?:use_step_matcher|step_matcher)\s*\(\s*["'](\w+)["']\s*\)/g; +const PARSER = /^(?:parsers\.)?(parse|cfparse|re|string)\s*\(\s*/; + +/** The step definitions of a Python file. */ +export function pythonStepDefinitions(text: string): StepDefinition[] { + const framework = pythonFramework(text); + // behave's matcher in effect from each `use_step_matcher(…)` call on. + const switches = [...text.matchAll(STEP_MATCHER)].map((m) => ({ + at: m.index, + matcher: (m[1] === "re" ? "re" : "parse") as Matcher, + })); + const out: StepDefinition[] = []; + for (const m of text.matchAll(DECORATOR)) { + let at = m.index + m[0].length; + let matcher: Matcher = + framework === "pytest-bdd" ? "string" : (switches.filter((s) => s.at < m.index).pop()?.matcher ?? "parse"); + const parser = PARSER.exec(text.slice(at, at + 32)); + if (parser) { + matcher = parser[1] === "re" ? "re" : parser[1] === "string" ? "string" : "parse"; + at += parser[0].length; + } + const literal = readPythonString(text, at); + if (!literal) continue; + const regexp = matcherRegExp(literal.value, matcher); + if (regexp) out.push({ from: at, to: literal.end, source: literal.value, regexp }); + } + return out; +} + +/** A string literal at `at` (an `r`/`u`/`b` prefix and triple quotes included), without f-strings. */ +export function readPythonString(text: string, at: number): { value: string; end: number } | null { + const prefix = /^[rRuUbB]{0,2}/.exec(text.slice(at, at + 2))![0]; + const raw = /r/i.test(prefix); + let i = at + prefix.length; + const quote = text[i]; + if (quote !== "'" && quote !== '"') return null; + const triple = text.startsWith(quote.repeat(3), i); + const close = triple ? quote.repeat(3) : quote; + i += close.length; + let value = ""; + for (; i < text.length; i++) { + if (text.startsWith(close, i)) return { value, end: i + close.length }; + const c = text[i]!; + if (c === "\\" && i + 1 < text.length) { + const next = text[i + 1]!; + // Raw strings keep the backslash; others resolve the escapes a pattern can contain. + value += raw ? c + next : next === "\\" || next === "'" || next === '"' ? next : next === "n" ? "\n" : c + next; + i++; + } else if (c === "\n" && !triple) return null; + else value += c; + } + return null; +} + +function matcherRegExp(value: string, matcher: Matcher): RegExp | null { + if (matcher === "string") return new RegExp(`^${escapeRe(value)}$`); + if (matcher === "parse") return parseFormatRegExp(value); + try { + // Python's named groups and back-references; steps match the whole text. + const source = value.replace(/\(\?P<(\w+)>/g, "(?<$1>").replace(/\(\?P=(\w+)\)/g, "\\k<$1>"); + return new RegExp(`^(?:${source})$`); + } catch { + return null; + } +} + +const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&"); + +const NUMBER = "[-+]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][-+]?\\d+)?"; + +/** The `parse` library's field types (`{count:d}`); others, and untyped fields, match any text. */ +const PARSE_TYPES: Record = { + d: "[-+]?\\d+", + n: "[-+]?\\d[\\d,]*", + f: NUMBER, + F: NUMBER, + e: NUMBER, + g: NUMBER, + w: "\\w+", + W: "\\W+", + s: "\\s+", + S: "\\S+", + D: "\\D+", + l: "[A-Za-z]+", +}; + +/** + * A `parse` format (`there are {count:d} cucumbers`) as an anchored, case-insensitive regular + * expression, as `parse` matches. `{{` and `}}` are literal braces; cfparse's cardinality + * (`{names:w+}`) matches any text. Null when a field is not closed. + */ +export function parseFormatRegExp(format: string): RegExp | null { + let out = ""; + for (let i = 0; i < format.length; i++) { + const c = format[i]!; + if ((c === "{" || c === "}") && format[i + 1] === c) { + out += `\\${c}`; + i++; + } else if (c === "{") { + const end = format.indexOf("}", i); + if (end < 0) return null; + const field = format.slice(i + 1, end); + const spec = field.includes(":") ? field.slice(field.indexOf(":") + 1) : ""; + out += `(${PARSE_TYPES[spec.slice(-1)] && !/[?*+]$/.test(spec) ? PARSE_TYPES[spec.slice(-1)] : ".+?"})`; + i = end; + } else out += escapeRe(c); + } + try { + return new RegExp(`^${out}$`, "i"); + } catch { + return null; + } +} + +/** + * The pending definition behave or pytest-bdd suggests for an undefined step, at the top level + * of the module. `body` is the line (0-based) the caret goes to. + */ +export function pythonStepSnippet( + keyword: string, + text: string, + framework: PythonFramework, +): { text: string; body: number } { + const decorator = keyword.toLowerCase(); + const quoted = (s: string) => `'${s.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`; + if (framework === "pytest-bdd") + return { text: [`@${decorator}(${quoted(text)})`, "def _():", " raise NotImplementedError", ""].join("\n"), body: 2 }; + // behave reads the pattern as a `parse` format: braces in the step are literal. + const format = text.replace(/[{}]/g, "$&$&"); + return { + text: [ + `@${decorator}(${quoted(format)})`, + "def step_impl(context):", + ` raise NotImplementedError(${quoted(`STEP: ${keyword} ${text}`)})`, + "", + ].join("\n"), + body: 2, + }; +} + +/** The `from behave import …` (or `pytest_bdd`) line's insertion that adds `keyword`, if one is missing. */ +export function pythonImportInsertion( + fileText: string, + keyword: string, + framework: PythonFramework, +): { at: number; text: string } | null { + const module = framework === "pytest-bdd" ? "pytest_bdd" : "behave"; + const line = new RegExp(`^from[ \\t]+${module}[ \\t]+import[ \\t]+([\\w ,\\t]+?)[ \\t]*$`, "m").exec(fileText); + if (!line) return null; + const names = line[1]!.split(",").map((n) => n.trim()); + const name = keyword.toLowerCase(); + if (names.includes(name) || names.includes("*")) return null; + return { at: line.index + line[0].length, text: `, ${name}` }; +} diff --git a/src/features/cucumber/stepIndex.ts b/src/features/cucumber/stepIndex.ts index 4a9d591bb..a30557965 100644 --- a/src/features/cucumber/stepIndex.ts +++ b/src/features/cucumber/stepIndex.ts @@ -35,7 +35,7 @@ export async function dialectOf(text: string): Promise { /** Whether a change to `path` can change the index: a feature or script file. */ export function isCucumberSource(path: string): boolean { - return /\.(feature|[cm]?[jt]sx?|java|kt)$/i.test(path); + return /\.(feature|[cm]?[jt]sx?|java|kt|py)$/i.test(path); } /** Forgets the index, so the next lookup rescans the project. */ @@ -51,7 +51,7 @@ function stepIndex(): Promise { const path = ipcPath(s.path); if (s.feature) index.steps.push({ path, text: s.text, items: featureSteps(s.text, await dialectOf(s.text)) }); else { - const items = stepDefinitions(s.text); + const items = stepDefinitions(s.text, path); if (items.length) index.definitions.push({ path, text: s.text, items }); } } @@ -110,13 +110,15 @@ export async function undefinedSteps(steps: readonly FeatureStep[]): Promise { const { definitions } = await stepIndex(); - // `cart.steps.ts`, `CartSteps.java` and `cart_stepdefs.kt` all name the `cart` feature's steps. + // `cart.steps.ts`, `CartSteps.java`, `cart_stepdefs.kt` and pytest-bdd's `test_cart.py` all + // name the `cart` feature's steps. const stem = (path: string) => (path.split("/").pop() ?? "") .split(".")[0]! .toLowerCase() .replace(/[^a-z0-9]/g, "") - .replace(/(?:stepdefinitions|stepdefs|steps)$/, ""); + .replace(/(?:stepdefinitions|stepdefs|steps)$/, "") + .replace(/^test(?=.)/, ""); const feature = stem(featurePath); const named = definitions.find((file) => stem(file.path) === feature); if (named) return named.path; diff --git a/src/features/cucumber/steps.ts b/src/features/cucumber/steps.ts index 2e7265f64..aa26ed445 100644 --- a/src/features/cucumber/steps.ts +++ b/src/features/cucumber/steps.ts @@ -2,8 +2,15 @@ * Cucumber steps, both sides: the step definitions of JavaScript / TypeScript files * (`Given('a {int} item cart', …)` of `@cucumber/cucumber`, `playwright-bdd`'s `createBdd()` * functions) and the steps of Gherkin `.feature` files, with Cucumber expression and regular - * expression matching between them. + * expression matching between them. Python files are read by `pythonSteps`. */ +import { + pythonFramework, + pythonImportInsertion, + pythonStepDefinitions, + pythonStepSnippet, + type PythonFramework, +} from "./pythonSteps"; /** * A step definition: the pattern literal of a `Given` / `When` / `Then` / `Step` call, or of a @@ -35,8 +42,9 @@ export interface FeatureStep { const DEFINE = /\b(?:Given|When|Then|And|But|Step|defineStep)\s*(?:<[^<>()]*>\s*)?\(\s*/g; -/** The step definitions of a script. */ -export function stepDefinitions(text: string): StepDefinition[] { +/** The step definitions of a script; `path` tells a Python file from the others. */ +export function stepDefinitions(text: string, path = ""): StepDefinition[] { + if (/\.py$/i.test(path)) return pythonStepDefinitions(text); const out: StepDefinition[] = []; for (const m of text.matchAll(DEFINE)) { const at = m.index + m[0].length; @@ -303,12 +311,15 @@ export function definingKeyword(steps: readonly FeatureStep[], step: FeatureStep /** Strings and numbers of a step's text, as Cucumber's snippets turn them into parameters. */ const SNIPPET_PARAMETER = /(^|[^\w.-])("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|-?\d+\.\d+|-?\d+)(?![\w.])/g; -export type SnippetStyle = "cucumber" | "playwright-bdd" | "java" | "kotlin"; +export type SnippetStyle = "cucumber" | "playwright-bdd" | "java" | "kotlin" | PythonFramework; + +const isPython = (style: SnippetStyle): style is PythonFramework => style === "behave" || style === "pytest-bdd"; /** The snippet style of a step definition file. */ export function snippetStyle(path: string, text: string): SnippetStyle { if (/\.java$/i.test(path)) return "java"; if (/\.kts?$/i.test(path)) return "kotlin"; + if (/\.py$/i.test(path)) return pythonFramework(text); return /\bcreateBdd\s*\(/.test(text) ? "playwright-bdd" : "cucumber"; } @@ -351,6 +362,7 @@ export function stepSnippet( text: string, style: SnippetStyle = "cucumber", ): { text: string; body: number } { + if (isPython(style)) return pythonStepSnippet(keyword, text, style); const jvm = style === "java" || style === "kotlin"; const types: string[] = []; let expression = ""; @@ -457,7 +469,12 @@ export function definitionInsertions( } } else { at = fileText.length; - const lead = !fileText ? "" : fileText.endsWith("\n\n") ? "" : fileText.endsWith("\n") ? "\n" : "\n\n"; + const imported = isPython(style) ? pythonImportInsertion(fileText, keyword, style) : null; + if (imported) insertions.push(imported); + // Python separates top-level functions with two blank lines. + const gap = isPython(style) ? "\n\n\n" : "\n\n"; + const ending = /\n*$/.exec(fileText)![0].length; + const lead = !fileText ? "" : gap.slice(Math.min(ending, gap.length)); insert = lead + snippet.text; } insertions.push({ at, text: insert });