From 896d2b818b50ff704f8c3fe19ace3fca6b6c067f Mon Sep 17 00:00:00 2001 From: William Killerud Date: Sat, 18 Nov 2023 19:20:38 +0100 Subject: [PATCH] test: add tests for hide and show Fix a regression in hide functionality --- .../multi-level-hide/colors/_index.scss | 1 + .../multi-level-hide/colors/base/_base.scss | 3 + .../multi-level-hide/colors/base/_index.scss | 1 + .../completion/multi-level-hide/styles.scss | 3 + .../same-symbol-name-hide/colors/_index.scss | 2 + .../colors/branch-a/_base.scss | 1 + .../colors/branch-a/_index.scss | 1 + .../colors/branch-b/_base.scss | 1 + .../colors/branch-b/_index.scss | 1 + .../same-symbol-name-hide/styles.scss | 3 + .../same-symbol-name-show/colors/_index.scss | 2 + .../colors/branch-a/_base.scss | 3 + .../colors/branch-a/_index.scss | 1 + .../colors/branch-b/_base.scss | 3 + .../colors/branch-b/_index.scss | 1 + .../same-symbol-name-show/styles.scss | 3 + server/src/features/completion/completion.ts | 6 +- .../features/completion-visibility.spec.ts | 89 +++++++++++++++++++ .../scanner-helper.ts => fixture-helper.ts} | 2 +- server/src/test/scanner/scanner.spec.ts | 2 +- 20 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 fixtures/unit/completion/multi-level-hide/colors/_index.scss create mode 100644 fixtures/unit/completion/multi-level-hide/colors/base/_base.scss create mode 100644 fixtures/unit/completion/multi-level-hide/colors/base/_index.scss create mode 100644 fixtures/unit/completion/multi-level-hide/styles.scss create mode 100644 fixtures/unit/completion/same-symbol-name-hide/colors/_index.scss create mode 100644 fixtures/unit/completion/same-symbol-name-hide/colors/branch-a/_base.scss create mode 100644 fixtures/unit/completion/same-symbol-name-hide/colors/branch-a/_index.scss create mode 100644 fixtures/unit/completion/same-symbol-name-hide/colors/branch-b/_base.scss create mode 100644 fixtures/unit/completion/same-symbol-name-hide/colors/branch-b/_index.scss create mode 100644 fixtures/unit/completion/same-symbol-name-hide/styles.scss create mode 100644 fixtures/unit/completion/same-symbol-name-show/colors/_index.scss create mode 100644 fixtures/unit/completion/same-symbol-name-show/colors/branch-a/_base.scss create mode 100644 fixtures/unit/completion/same-symbol-name-show/colors/branch-a/_index.scss create mode 100644 fixtures/unit/completion/same-symbol-name-show/colors/branch-b/_base.scss create mode 100644 fixtures/unit/completion/same-symbol-name-show/colors/branch-b/_index.scss create mode 100644 fixtures/unit/completion/same-symbol-name-show/styles.scss create mode 100644 server/src/test/features/completion-visibility.spec.ts rename server/src/test/{scanner/scanner-helper.ts => fixture-helper.ts} (72%) diff --git a/fixtures/unit/completion/multi-level-hide/colors/_index.scss b/fixtures/unit/completion/multi-level-hide/colors/_index.scss new file mode 100644 index 00000000..24240d5b --- /dev/null +++ b/fixtures/unit/completion/multi-level-hide/colors/_index.scss @@ -0,0 +1 @@ +@forward "./base" hide $color-white; diff --git a/fixtures/unit/completion/multi-level-hide/colors/base/_base.scss b/fixtures/unit/completion/multi-level-hide/colors/base/_base.scss new file mode 100644 index 00000000..ca1785b5 --- /dev/null +++ b/fixtures/unit/completion/multi-level-hide/colors/base/_base.scss @@ -0,0 +1,3 @@ +$color-black: black; +$color-grey: grey; +$color-white: white; diff --git a/fixtures/unit/completion/multi-level-hide/colors/base/_index.scss b/fixtures/unit/completion/multi-level-hide/colors/base/_index.scss new file mode 100644 index 00000000..6b08d505 --- /dev/null +++ b/fixtures/unit/completion/multi-level-hide/colors/base/_index.scss @@ -0,0 +1 @@ +@forward "./base" hide $color-black; diff --git a/fixtures/unit/completion/multi-level-hide/styles.scss b/fixtures/unit/completion/multi-level-hide/styles.scss new file mode 100644 index 00000000..66ca69e6 --- /dev/null +++ b/fixtures/unit/completion/multi-level-hide/styles.scss @@ -0,0 +1,3 @@ +@use "./colors"; + +$text-color: colors.| diff --git a/fixtures/unit/completion/same-symbol-name-hide/colors/_index.scss b/fixtures/unit/completion/same-symbol-name-hide/colors/_index.scss new file mode 100644 index 00000000..88cf8e2e --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-hide/colors/_index.scss @@ -0,0 +1,2 @@ +@forward "./branch-a" hide $color-white; +@forward "./branch-b"; diff --git a/fixtures/unit/completion/same-symbol-name-hide/colors/branch-a/_base.scss b/fixtures/unit/completion/same-symbol-name-hide/colors/branch-a/_base.scss new file mode 100644 index 00000000..933650ff --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-hide/colors/branch-a/_base.scss @@ -0,0 +1 @@ +$color-white: white; diff --git a/fixtures/unit/completion/same-symbol-name-hide/colors/branch-a/_index.scss b/fixtures/unit/completion/same-symbol-name-hide/colors/branch-a/_index.scss new file mode 100644 index 00000000..e1106df5 --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-hide/colors/branch-a/_index.scss @@ -0,0 +1 @@ +@forward "./base"; diff --git a/fixtures/unit/completion/same-symbol-name-hide/colors/branch-b/_base.scss b/fixtures/unit/completion/same-symbol-name-hide/colors/branch-b/_base.scss new file mode 100644 index 00000000..933650ff --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-hide/colors/branch-b/_base.scss @@ -0,0 +1 @@ +$color-white: white; diff --git a/fixtures/unit/completion/same-symbol-name-hide/colors/branch-b/_index.scss b/fixtures/unit/completion/same-symbol-name-hide/colors/branch-b/_index.scss new file mode 100644 index 00000000..e1106df5 --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-hide/colors/branch-b/_index.scss @@ -0,0 +1 @@ +@forward "./base"; diff --git a/fixtures/unit/completion/same-symbol-name-hide/styles.scss b/fixtures/unit/completion/same-symbol-name-hide/styles.scss new file mode 100644 index 00000000..66ca69e6 --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-hide/styles.scss @@ -0,0 +1,3 @@ +@use "./colors"; + +$text-color: colors.| diff --git a/fixtures/unit/completion/same-symbol-name-show/colors/_index.scss b/fixtures/unit/completion/same-symbol-name-show/colors/_index.scss new file mode 100644 index 00000000..d0b8a13e --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-show/colors/_index.scss @@ -0,0 +1,2 @@ +@forward "./branch-a" show $color-black; +@forward "./branch-b"; diff --git a/fixtures/unit/completion/same-symbol-name-show/colors/branch-a/_base.scss b/fixtures/unit/completion/same-symbol-name-show/colors/branch-a/_base.scss new file mode 100644 index 00000000..ca1785b5 --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-show/colors/branch-a/_base.scss @@ -0,0 +1,3 @@ +$color-black: black; +$color-grey: grey; +$color-white: white; diff --git a/fixtures/unit/completion/same-symbol-name-show/colors/branch-a/_index.scss b/fixtures/unit/completion/same-symbol-name-show/colors/branch-a/_index.scss new file mode 100644 index 00000000..e1106df5 --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-show/colors/branch-a/_index.scss @@ -0,0 +1 @@ +@forward "./base"; diff --git a/fixtures/unit/completion/same-symbol-name-show/colors/branch-b/_base.scss b/fixtures/unit/completion/same-symbol-name-show/colors/branch-b/_base.scss new file mode 100644 index 00000000..ca1785b5 --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-show/colors/branch-b/_base.scss @@ -0,0 +1,3 @@ +$color-black: black; +$color-grey: grey; +$color-white: white; diff --git a/fixtures/unit/completion/same-symbol-name-show/colors/branch-b/_index.scss b/fixtures/unit/completion/same-symbol-name-show/colors/branch-b/_index.scss new file mode 100644 index 00000000..e1106df5 --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-show/colors/branch-b/_index.scss @@ -0,0 +1 @@ +@forward "./base"; diff --git a/fixtures/unit/completion/same-symbol-name-show/styles.scss b/fixtures/unit/completion/same-symbol-name-show/styles.scss new file mode 100644 index 00000000..66ca69e6 --- /dev/null +++ b/fixtures/unit/completion/same-symbol-name-show/styles.scss @@ -0,0 +1,3 @@ +@use "./colors"; + +$text-color: colors.| diff --git a/server/src/features/completion/completion.ts b/server/src/features/completion/completion.ts index 13803c30..99010b7b 100644 --- a/server/src/features/completion/completion.ts +++ b/server/src/features/completion/completion.ts @@ -305,10 +305,6 @@ function traverseTree( accumulator.set(leaf.uri, completionItems); - // Reset these once we've processed the document they refered to - let hidden: string[] = []; - let shown: string[] = []; - // Check to see if we have to go deeper // Don't follow uses, since we start with the document behind the first use, and symbols from further uses aren't available to us // Don't follow imports, since the whole point here is to use the new module system @@ -322,6 +318,8 @@ function traverseTree( continue; } + let hidden = hiddenSymbols; + let shown = shownSymbols; if ( (child as ScssForward).hide && (child as ScssForward).hide.length > 0 diff --git a/server/src/test/features/completion-visibility.spec.ts b/server/src/test/features/completion-visibility.spec.ts new file mode 100644 index 00000000..503b0b46 --- /dev/null +++ b/server/src/test/features/completion-visibility.spec.ts @@ -0,0 +1,89 @@ +import * as assert from "assert"; +import { changeConfiguration, useContext } from "../../context-provider"; +import { doCompletion } from "../../features/completion"; +import { NodeFileSystem } from "../../node-file-system"; +import { IScssDocument } from "../../parser"; +import ScannerService from "../../scanner"; +import { getUri } from "../fixture-helper"; +import * as helpers from "../helpers"; + +describe("Providers/Completion", () => { + beforeEach(async () => { + helpers.createTestContext(new NodeFileSystem()); + + const settings = helpers.makeSettings({ + suggestFromUseOnly: true, + }); + changeConfiguration(settings); + }); + + describe("Hide", () => { + it("supports multi-level hiding", async () => { + const workspaceUri = getUri("completion/multi-level-hide/"); + const docUri = getUri("completion/multi-level-hide/styles.scss"); + const scanner = new ScannerService(); + await scanner.scan([docUri], workspaceUri); + const { storage } = useContext(); + const stylesDoc = storage.get(docUri) as IScssDocument; + + const completions = await doCompletion( + stylesDoc, + stylesDoc.getText().indexOf("|"), + ); + + // $color-black and $color-white are hidden at different points + + assert.equal( + completions.items.length, + 1, + "Expected only one suggestion from the multi-level-hide fixture", + ); + assert.equal(completions.items[0].label, "$color-grey"); + }); + + it("doesn't hide symbol with same name in different part of dependency graph", async () => { + const workspaceUri = getUri("completion/same-symbol-name-hide/"); + const docUri = getUri("completion/same-symbol-name-hide/styles.scss"); + const scanner = new ScannerService(); + await scanner.scan([docUri], workspaceUri); + const { storage } = useContext(); + const stylesDoc = storage.get(docUri) as IScssDocument; + + const completions = await doCompletion( + stylesDoc, + stylesDoc.getText().indexOf("|"), + ); + + // $color-white is hidden in branch-a, but not in branch-b + assert.equal( + completions.items.length, + 1, + "Expected a suggestion from the same-symbol-name-hide fixture", + ); + assert.equal(completions.items[0].label, "$color-white"); + }); + }); + + describe("Show", () => { + it("doesn't show symbol with same name in different part of dependency graph", async () => { + const workspaceUri = getUri("completion/same-symbol-name-show/"); + const docUri = getUri("completion/same-symbol-name-show/styles.scss"); + const scanner = new ScannerService(); + await scanner.scan([docUri], workspaceUri); + const { storage } = useContext(); + const stylesDoc = storage.get(docUri) as IScssDocument; + + const completions = await doCompletion( + stylesDoc, + stylesDoc.getText().indexOf("|"), + ); + + // One branch only shows $color-black, but the other has three symbols including another $color-black + assert.equal( + completions.items.length, + 4, + "Expected four suggestions from the same-symbol-name-show fixture", + ); + }); + }); +}); diff --git a/server/src/test/scanner/scanner-helper.ts b/server/src/test/fixture-helper.ts similarity index 72% rename from server/src/test/scanner/scanner-helper.ts rename to server/src/test/fixture-helper.ts index 8269017b..d3d663f9 100644 --- a/server/src/test/scanner/scanner-helper.ts +++ b/server/src/test/fixture-helper.ts @@ -2,7 +2,7 @@ import * as path from "path"; import { URI } from "vscode-uri"; function getDocPath(p: string) { - return path.resolve(__dirname, "../../../../fixtures/unit", p); + return path.resolve(__dirname, "../../../fixtures/unit", p); } export function getUri(p: string) { diff --git a/server/src/test/scanner/scanner.spec.ts b/server/src/test/scanner/scanner.spec.ts index 1cbf612e..40e4fc31 100644 --- a/server/src/test/scanner/scanner.spec.ts +++ b/server/src/test/scanner/scanner.spec.ts @@ -3,8 +3,8 @@ import { isMatch } from "micromatch"; import { useContext } from "../../context-provider"; import { NodeFileSystem } from "../../node-file-system"; import ScannerService from "../../scanner"; +import { getUri } from "../fixture-helper"; import * as helpers from "../helpers"; -import { getUri } from "./scanner-helper"; describe("Services/Scanner", () => { beforeEach(() => {