diff --git a/.dependency-cruiser.js b/.dependency-cruiser.js index 98fd858..3b4ed76 100644 --- a/.dependency-cruiser.js +++ b/.dependency-cruiser.js @@ -1,6 +1,24 @@ /** @type {import('dependency-cruiser').IConfiguration} */ module.exports = { forbidden: [ + /* architecture of this extension: */ + { + name: 'core-independent-of-vscode', + severity: 'error', + comment: + 'The core contains the yaml logic and must run without VS Code, so it can be unit tested in plain node ' + + 'and reused. Talk to VS Code from src/vscode instead and pass the values into the core.', + from: { path: '^src/core' }, + to: { path: '^node_modules/@types/vscode/|^src/vscode/|^src/extension' } + }, + { + name: 'unit-tests-independent-of-vscode', + severity: 'error', + comment: + 'Unit tests run in plain node (npm run test:unit). Tests that need VS Code belong to src/test/integration.', + from: { path: '^src/test/unit' }, + to: { path: '^node_modules/@types/vscode/|^src/vscode/|^src/extension' } + }, /* rules from the 'recommended' preset: */ { name: 'no-circular', diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 3e19a43..286289b 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -25,10 +25,7 @@ jobs: node-version: ${{env.NODE_VERSION}} - name: install - run: | - npm install - npm install -g yarn - npm install -g vsce + run: npm ci - name: lint run: npm run lint @@ -36,6 +33,9 @@ jobs: - name: static code analysis run: npm run code-analysis + - name: unit tests + run: npm run test:unit + - name: test uses: GabrielBB/xvfb-action@v1 with: diff --git a/.vscode/launch.json b/.vscode/launch.json index 9528312..c066745 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -20,7 +20,7 @@ "runtimeExecutable": "${execPath}", "args": [ "--extensionDevelopmentPath=${workspaceFolder}", - "--extensionTestsPath=${workspaceFolder}/out/test" + "--extensionTestsPath=${workspaceFolder}/out/test/integration/index" ], "outFiles": [ "${workspaceFolder}/out/test/**/*.js" diff --git a/CHANGELOG.md b/CHANGELOG.md index 90c9327..89d32db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ ## Unreleased 🐛 Bugfixes +* Command `Format YAML` with a selection formats the whole document. Before, it replaced the selected lines with the formatted document, so the rest of the document appeared twice. The editor commands apply their changes to the document they were run on and show the success message only after VS Code applied them. Running a command without an open editor shows a hint instead of an internal error +* Keep a comment at its key when sorting changes the key's value, e.g. removes its quotes. Before, the comment could move to another key that contains the key's name, like `data` for `a` +* Keep tabs inside values, e.g. in a quoted string. Only tabs in the indentation are replaced with spaces +* Keep the leading zero of octal values like `0755` that are followed by a comment or are items of a list. Before, they became `755` +* Command `Recursively sort YAML files` skips the folders `node_modules` and `.git`, so it no longer rewrites the yaml files of installed dependencies. Files that are already sorted are no longer written, and a file is processed once even if its extension is configured twice +* Sorting an indented selection no longer adds trailing whitespaces to its empty lines +* Setting `emptyLinesUntilLevel` no longer adds empty lines inside block scalars (`key: |`), which changed their value, and no longer separates a comment from the key below it +* Custom sort writes the keywords like every other key: a keyword that needs quotes is quoted (before, `'a: b': 1` became the invalid `a: b: 1`), a value containing a colon stays on the line of its keyword instead of moving to the next line, and the result no longer ends with an extra line break when every key is a keyword +* Keep a document that only contains an empty mapping (`{}`) when sorting or formatting. Before, it became an empty document +* Keep the inline comment of a value that contains an apostrophe (`key: it's # comment`) or an escaped quote (`key: "a \" b" # comment`). Before, the comment was dropped while sorting +* Sorting a whole document whose last key has no value (e.g. `key:`) no longer fails with "YAML selection is invalid". This check now only applies to selections * The editor commands (sort, custom sort, format, validate) are only offered in the command palette for yaml files. The condition was declared at a place VS Code ignores * (refs [#65](https://github.com/pascalre/vscode-yaml-sort/issues/65)) Formatting via "Format Document" or on save no longer shows "Yaml formatted successfully", once per document. The command `Format YAML` shows it once after the changes were applied * Command `Recursively sort YAML files` now sorts files with several documents, works without an open editor and is only offered for folders in the explorer. A file that cannot be sorted is reported and left unchanged. Files are now also found on Windows paths @@ -30,6 +41,8 @@ * The extension is only activated when a yaml file is opened or one of its commands is run, instead of on every start of VS Code 📦 Dependencies * Move `@types/js-yaml` and `@vscode/test-electron` to `devDependencies`, so they are no longer shipped with the extension +🏗️ Internal +* The sources are split into `src/core` (yaml logic, no VS Code API) and `src/vscode` (commands, formatter, notifications). The core reports problems by throwing errors, the vscode layer shows them. `npm run code-analysis` enforces the split, and `npm run test:unit` runs the core's tests in plain node within seconds # 6.6.4 - Feb 9, 2026 📦 Dependencies diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 99c4fc1..7ebc6cb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,10 +7,22 @@ When opening an issue, describe your issue as clear as possible. If useful, prov ## Workflow This project uses the [GitHub Flow](https://docs.github.com/en/get-started/quickstart/github-flow). +### Architecture +The sources are split into two layers: + +| Folder | Content | May use the VS Code API | +| --- | --- | --- | +| `src/core` | Sorting, formatting and validating yaml: settings, documents, processors, js-yaml | no | +| `src/vscode` | Commands, formatter, notifications, reading the configuration | yes | + +`src/extension.ts` registers the commands and the formatter. The core reports problems by throwing errors, the +vscode layer shows them to the user. `npm run code-analysis` fails if the core or a unit test imports VS Code. + ### Testing & Linting The following commands support you at developing good code: -* `npm run code-analysis`: Run the static code analysis -* `npm run test`: Run the tests +* `npm run code-analysis`: Run the static code analysis, including the layer rules above +* `npm run test:unit`: Run the unit tests of the core in plain node, which takes a few seconds +* `npm run test`: Run all tests, including the integration tests in `src/test/integration`, inside VS Code * `npm run lint`: Run the linter ### Commit changes diff --git a/README.md b/README.md index 6f852f2..9e8f912 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This extension contributes the following commands: | `Custom Sort 2` | Same as `Custom Sort 1` | | `Custom Sort 3` | Same as `Custom Sort 1` | | `Format Document` | Formats a yaml document without sorting it. Also possible using the shortcut (⇧⌥F on Mac). | -| `Recursively sort YAML files` | Sorts all `.yaml` and `.yml` files in a directory and all its subdirectories, including files with several documents. Run it from the context menu of a folder in the explorer. A newline at the end of a file will be retained. | +| `Recursively sort YAML files` | Sorts all `.yaml` and `.yml` files in a directory and all its subdirectories, including files with several documents. Run it from the context menu of a folder in the explorer. A newline at the end of a file will be retained. Files in `node_modules` and `.git` are skipped, files that are already sorted are not rewritten. | | `Sort YAML` | Sorts a given YAML. You can either sort the whole YAML document or sort only a selection of the text. | | `Validate YAML` | Validates a given YAML. | diff --git a/package.json b/package.json index 10b13c4..c157835 100644 --- a/package.json +++ b/package.json @@ -333,6 +333,8 @@ "pretest": "npm run compile", "sonar": "node sonar-project.js", "test": "node ./out/test/runTest.js", + "pretest:unit": "npm run compile", + "test:unit": "mocha --ui tdd \"out/test/unit/**/*.test.js\"", "vscode:prepublish": "npm run compile", "watch": "tsc -watch -p ./" }, diff --git a/src/adapter/vs-code-adapter.ts b/src/adapter/vs-code-adapter.ts deleted file mode 100644 index d3fea80..0000000 --- a/src/adapter/vs-code-adapter.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { Disposable, DocumentFormattingEditProvider, Position, Range, Selection, TextEdit, TextEditor, WorkspaceEdit, languages, window, workspace } from "vscode" - -import { Settings } from "../settings" - -export enum Severity { - INFO, ERROR -} - -export class VsCodeAdapter { - settings: Settings - - constructor(settings = new Settings()) { - this.settings = settings - } - - showMessage(severity: Severity, message: string) { - if (severity === Severity.ERROR) { - window.showErrorMessage(message) - } else { - this.notify(message) - } - } - - notify(message: string) { - if (this.settings.notifySuccess) { - window.showInformationMessage(message) - } - } - - static registerFormatter(formatter: DocumentFormattingEditProvider): Disposable { - return languages.registerDocumentFormattingEditProvider("yaml", formatter) - } - - static getText(textEditor: TextEditor, range: Range) { - return textEditor.document.getText(range) - } - - static getFullDocumentRange(textEditor: TextEditor) { - return new Range( - new Position(0, 0), - new Position(textEditor.document.lineCount + 1, 0)) - } - - static getSelectedRange(textEditor: TextEditor) { - let endLine = textEditor.selection.end.line - // if selection ends on the first character on a new line ignore this line - if (textEditor.selection.end.character === 0) { - endLine-- - } - - // ensure selection covers whole start and end line - return new Selection( - textEditor.selection.start.line, 0, - endLine, textEditor.document.lineAt(endLine).range.end.character) - } - - /** - * Applys edits to a text editor - * @param activeEditor Editor to apply the changes - * @param edits Changes to apply - */ - static applyEdits(edit: [TextEdit]) { - if (window.activeTextEditor) { - const workspaceEdit = new WorkspaceEdit() - workspaceEdit.set(window.activeTextEditor.document.uri, edit) - workspace.applyEdit(workspaceEdit) - } - } - - static getActiveDocument(textEditor: TextEditor) { - return textEditor.document.getText() - } - - static getRange(textEditor: TextEditor) { - if (textEditor.selection.isEmpty) { - return VsCodeAdapter.getFullDocumentRange(textEditor) - } else { - return VsCodeAdapter.getSelectedRange(textEditor) - } - } - - static getEdits(textEditor: TextEditor, text: string) { - const range = VsCodeAdapter.getRange(textEditor) - return TextEdit.replace(range, text) - } - -} diff --git a/src/controller/controller.ts b/src/controller/controller.ts deleted file mode 100644 index bae3ba5..0000000 --- a/src/controller/controller.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { TextEdit, TextEditor, window } from "vscode" - -import { formatDocuments, sortDocuments } from "./document-transformer" -import { JsYamlAdapter } from "../adapter/js-yaml-adapter" -import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter" -import { Settings } from "../settings" -import { ErrorUtil } from "../util/error-util" -import { YamlUtil } from "../util/yaml-util" - -export class Controller { - editor: TextEditor - settings: Settings - yamlutil: YamlUtil - jsyamladapter: JsYamlAdapter - vscodeadapter: VsCodeAdapter - errorutil = new ErrorUtil() - - constructor() { - if (!window.activeTextEditor) { - throw new Error("No texteditor is active") - } - this.editor = window.activeTextEditor - // one snapshot of the settings that apply to the edited document - this.settings = Settings.forDocument(this.editor.document) - this.yamlutil = new YamlUtil(this.settings) - this.jsyamladapter = new JsYamlAdapter(this.settings) - this.vscodeadapter = new VsCodeAdapter(this.settings) - } - - validateYamlWrapper(): boolean { - const text = VsCodeAdapter.getActiveDocument(this.editor) - try { - this.jsyamladapter.validateYaml(text) - this.vscodeadapter.showMessage(Severity.INFO, "YAML is valid.") - return true - } catch (error) { - this.errorutil.handleError(error) - } - return false - } - - formatYamlWrapper(): TextEdit[] { - const newText = formatDocuments(VsCodeAdapter.getActiveDocument(this.editor), this.yamlutil) - if (newText === null) { - return [] - } - const edits = this.applyEdits(newText) - // only the command reports success, the formatter stays silent (see #65) - this.vscodeadapter.showMessage(Severity.INFO, "Yaml formatted successfully") - return edits - } - - applyEdits(text: string) { - const edits = VsCodeAdapter.getEdits(this.editor, text) - VsCodeAdapter.applyEdits([edits]) - return [edits] - } - - sortYamlWrapper(customSort = 0): TextEdit[] { - const text = VsCodeAdapter.getText(this.editor, VsCodeAdapter.getRange(this.editor)) - let newText: string | null - try { - newText = sortDocuments(text, { customSort, isSelection: !this.editor.selection.isEmpty }, this.yamlutil) - } catch (error) { - this.errorutil.handleError(error) - return [] - } - if (newText === null) { - return [] - } - const edits = this.applyEdits(newText) - this.vscodeadapter.showMessage(Severity.INFO, "Keys resorted successfully") - return edits - } -} diff --git a/src/controller/file-sorter.ts b/src/controller/file-sorter.ts deleted file mode 100644 index 406a24b..0000000 --- a/src/controller/file-sorter.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { readFileSync, writeFileSync } from "fs" - -import { Uri } from "vscode" - -import { YamlOperations, sortDocuments } from "./document-transformer" -import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter" -import { Settings } from "../settings" -import { ErrorUtil } from "../util/error-util" -import { FileUtil } from "../util/file-util" -import { YamlUtil } from "../util/yaml-util" - -/** - * Sorts all yaml documents of a file and writes the result back. - * A trailing line break of the original file is retained (see #186). - * @param file Path of the file. - * @param yaml Yaml operations, injectable for tests. - * @throws Error if the file cannot be read, is not valid yaml or cannot be sorted. - */ -export const sortFile = (file: string, yaml: YamlOperations = new YamlUtil()): void => { - const text = readFileSync(file, "utf-8") - let sorted = sortDocuments(text, { customSort: 0, isSelection: false }, yaml) - if (sorted === null) { - throw new Error(`File ${file} could not be sorted`) - } - if (text.endsWith("\n") && !sorted.endsWith("\n")) { - sorted += "\n" - } - writeFileSync(file, sorted) -} - -/** - * Command handler: sorts every yaml file in a directory and its subdirectories. - * A file that cannot be sorted is reported and left unchanged; the other files are still sorted. - * @param uri The directory selected in the explorer. Undefined when the command is run from the command palette. - */ -export const sortYamlFilesInDirectory = (uri: Uri | undefined): void => { - const errorutil = new ErrorUtil() - if (!uri) { - new VsCodeAdapter().showMessage(Severity.ERROR, "Please run this command from the context menu of a folder in the explorer.") - return - } - const settingsFor = (resource: Uri) => Settings.forDocument({ uri: resource, languageId: "yaml" }) - for (const file of new FileUtil(settingsFor(uri)).getFiles(uri.fsPath)) { - try { - sortFile(file, new YamlUtil(settingsFor(Uri.file(file)))) - } catch (error) { - errorutil.handleError(error) - } - } -} diff --git a/src/controller/processor-controller.ts b/src/controller/processor-controller.ts deleted file mode 100644 index b7fcf94..0000000 --- a/src/controller/processor-controller.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { ArrayProcessor } from "../processor/array-processor" -import { BlockProcessor } from "../processor/block-processor" -import { CommentProcessor } from "../processor/comment-processor" -import { HelmProcessor } from "../processor/helm-processor" -import { OctalProcessor } from "../processor/octal-processor" -import { SpacingProcessor } from "../processor/spacing-processor" -import { TabsToSpacesProcessor } from "../processor/tabs-to-spaces-processor" -import { Settings } from "../settings" - -export class ProcessorController { - arrayprocessor!: ArrayProcessor - blockprocessor!: BlockProcessor - commentprocessor!: CommentProcessor - helmprocessor!: HelmProcessor - octalprocessor!: OctalProcessor - spacingprocessor!: SpacingProcessor - tabstospacespreprocessor!: TabsToSpacesProcessor - text: string - settings: Settings - - constructor(text: string, settings = new Settings()) { - this.text = text - this.settings = settings - } - - preprocess() { - this.tabstospacespreprocessor = new TabsToSpacesProcessor(this.text, this.settings) - this.tabstospacespreprocessor.preprocess() - this.text = this.tabstospacespreprocessor.text - - if (this.settings.useBlockProcessor) { - this.blockprocessor = new BlockProcessor(this.text) - this.blockprocessor.preprocess() - this.text = this.blockprocessor.text - } - - if (this.settings.useArrayProcessor) { - this.arrayprocessor = new ArrayProcessor(this.text) - this.arrayprocessor.preprocess() - this.text = this.arrayprocessor.text - } - - if (this.settings.useHelmProcessor) { - this.helmprocessor = new HelmProcessor(this.text) - this.helmprocessor.preprocess() - this.text = this.helmprocessor.text - } - - if (this.settings.useOctalProcessor) { - this.octalprocessor = new OctalProcessor(this.text) - this.octalprocessor.preprocess() - this.text = this.octalprocessor.text - } - - if (this.settings.useCommentProcessor) { - this.commentprocessor = new CommentProcessor(this.text) - this.commentprocessor.findComments() - this.text = this.commentprocessor.text - } - } - - postprocess() { - if (this.settings.useCommentProcessor) { - this.commentprocessor.text = this.text - this.commentprocessor.postprocess() - this.text = this.commentprocessor.text - } - - if (this.settings.useOctalProcessor) { - this.octalprocessor.text = this.text - this.octalprocessor.postprocess() - this.text = this.octalprocessor.text - } - - if (this.settings.useHelmProcessor) { - this.helmprocessor.text = this.text - this.helmprocessor.postprocess() - this.text = this.helmprocessor.text - } - - if (this.settings.useArrayProcessor) { - this.arrayprocessor.text = this.text - this.arrayprocessor.postprocess() - this.text = this.arrayprocessor.text - } - - if (this.settings.useBlockProcessor) { - this.blockprocessor.text = this.text - this.blockprocessor.postprocess() - this.text = this.blockprocessor.text - } - - this.spacingprocessor = new SpacingProcessor(this.text, this.settings) - this.spacingprocessor.postprocess() - this.text = this.spacingprocessor.text - } -} diff --git a/src/controller/document-transformer.ts b/src/core/document-transformer.ts similarity index 57% rename from src/controller/document-transformer.ts rename to src/core/document-transformer.ts index bf7e9d2..dad7dec 100644 --- a/src/controller/document-transformer.ts +++ b/src/core/document-transformer.ts @@ -1,12 +1,12 @@ -import { JsYamlAdapter } from "../adapter/js-yaml-adapter" -import { prependWhitespacesOnEachLine, removeLeadingSpacesOnEachLine } from "../lib" -import { YamlDocument, YamlStream, splitDocuments, validateTextRange } from "../util/yaml-text" -import { YamlUtil } from "../util/yaml-util" +import { prependWhitespacesOnEachLine, removeLeadingSpacesOnEachLine } from "./indentation" +import { JsYamlAdapter } from "./js-yaml-adapter" +import { YamlSorter } from "./yaml-sorter" +import { YamlDocument, YamlStream, splitDocuments, validateTextRange } from "./yaml-text" /** - * The parts of YamlUtil a transformation depends on. Kept narrow so tests can pass a stub. + * The parts of YamlSorter a transformation depends on. Kept narrow so tests can pass a stub. */ -export type YamlOperations = Pick +export type YamlOperations = Pick export interface SortOptions { /** 0 for the default sort, 1 to 3 for the custom sort keywords. */ @@ -16,9 +16,10 @@ export interface SortOptions { } /** - * Transforms the content of a single, non-empty document. Returns null if that fails. + * Transforms the content of a single, non-empty document. + * @throws Error if the content cannot be transformed. */ -type ContentTransformation = (content: string) => string | null +type ContentTransformation = (content: string) => string /** * Puts the transformed documents back together. @@ -40,47 +41,38 @@ const joinDocuments = (stream: YamlStream, leadingDashes: boolean | undefined): /** * Applies a transformation to every non-empty document. Empty documents are kept as they are. - * @returns The transformed stream, or null if one document could not be transformed. + * The transformation throws for a document it cannot transform, so there never is a partial + * result that drops documents or empties a selection. + * @returns The transformed stream. */ -const transformDocuments = (stream: YamlStream, transform: ContentTransformation): YamlStream | null => { - const documents: YamlDocument[] = [] - for (const document of stream.documents) { - if (document.content.trim() === "") { - documents.push({ delimiter: document.delimiter, content: "" }) - continue - } - const content = transform(document.content) - if (!content) { - // never return a partial result: it would drop documents or empty the selection - return null - } - documents.push({ delimiter: document.delimiter, content }) - } - return { directives: stream.directives, documents } -} +const transformDocuments = (stream: YamlStream, transform: ContentTransformation): YamlStream => ({ + directives: stream.directives, + documents: stream.documents.map((document): YamlDocument => ({ + delimiter: document.delimiter, + content: document.content.trim() === "" ? "" : transform(document.content), + })), +}) /** * Sorts every yaml document in a text. * @param text Text containing one or more yaml documents. * @param options Sort options. * @param yaml Yaml operations, injectable for tests. - * @returns The sorted text, or null if a document could not be sorted - * (the error has already been reported to the user). - * @throws Error if the text is not valid yaml or the selection ends invalid. + * @returns The sorted text. + * @throws Error if the text is not valid yaml, a selection ends invalid or a document cannot be sorted. */ -export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlUtil()): string | null => { - validateTextRange(text) +export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOperations = new YamlSorter()): string => { + if (options.isSelection) { + validateTextRange(text) + } new JsYamlAdapter(yaml.settings).validateYaml(text) const sorted = transformDocuments(splitDocuments(text), (content) => { // an indented selection is sorted like a top-level document and indented again afterwards (see #53) - const indentation = options.isSelection ? YamlUtil.getNumberOfLeadingSpaces(content) : 0 + const indentation = options.isSelection ? YamlSorter.getNumberOfLeadingSpaces(content) : 0 const sortedContent = yaml.sortYaml(removeLeadingSpacesOnEachLine(content, indentation), options.customSort) - return sortedContent ? prependWhitespacesOnEachLine(sortedContent, indentation) : null + return prependWhitespacesOnEachLine(sortedContent, indentation) }) - if (!sorted) { - return null - } return joinDocuments(sorted, options.isSelection ? undefined : yaml.settings.useLeadingDashes) } @@ -88,13 +80,10 @@ export const sortDocuments = (text: string, options: SortOptions, yaml: YamlOper * Formats every yaml document in a text without sorting it. * @param text Text containing one or more yaml documents. * @param yaml Yaml operations, injectable for tests. - * @returns The formatted text, or null if a document could not be formatted - * (the error has already been reported to the user). + * @returns The formatted text. + * @throws Error if a document is not valid yaml. */ -export const formatDocuments = (text: string, yaml: YamlOperations = new YamlUtil()): string | null => { +export const formatDocuments = (text: string, yaml: YamlOperations = new YamlSorter()): string => { const formatted = transformDocuments(splitDocuments(text), (content) => yaml.formatYaml(content, false)) - if (!formatted) { - return null - } return joinDocuments(formatted, yaml.settings.useLeadingDashes) } diff --git a/src/core/file-sorter.ts b/src/core/file-sorter.ts new file mode 100644 index 0000000..495cd4a --- /dev/null +++ b/src/core/file-sorter.ts @@ -0,0 +1,23 @@ +import { readFileSync, writeFileSync } from "fs" + +import { YamlOperations, sortDocuments } from "./document-transformer" +import { YamlSorter } from "./yaml-sorter" + +/** + * Sorts all yaml documents of a file and writes the result back, if it differs. + * A trailing line break of the original file is retained (see #186). + * @param file Path of the file. + * @param yaml Yaml operations, injectable for tests. + * @throws Error if the file cannot be read, is not valid yaml or cannot be sorted. + */ +export const sortFile = (file: string, yaml: YamlOperations = new YamlSorter()): void => { + const text = readFileSync(file, "utf-8") + let sorted = sortDocuments(text, { customSort: 0, isSelection: false }, yaml) + if (text.endsWith("\n") && !sorted.endsWith("\n")) { + sorted += "\n" + } + // an unchanged file keeps its modification time, so watchers and build tools are not triggered + if (sorted !== text) { + writeFileSync(file, sorted) + } +} diff --git a/src/lib.ts b/src/core/indentation.ts similarity index 85% rename from src/lib.ts rename to src/core/indentation.ts index 930c9a0..368ebbb 100644 --- a/src/lib.ts +++ b/src/core/indentation.ts @@ -1,6 +1,6 @@ /** * Prepends a given count of whitespaces to every single line in a text. - * Lines with yaml seperators (---) will not be indented + * Lines with yaml separators (---) and empty lines will not be indented * @param {string} text Text which should get some leading whitespaces on each line. * @param {number} count The number of whitesspaces to prepend on each line of the returned string. * @returns {string} Input Text, which has the given count of whitespaces prepended on each single line. @@ -11,7 +11,8 @@ export function prependWhitespacesOnEachLine(text: string, count: number): strin } const spaces = " ".repeat(count) - return text.replace(/^(?!---)/mg, spaces) + // empty lines stay empty instead of getting trailing whitespaces + return text.replace(/^(?!---|$)/mg, spaces) } /** diff --git a/src/adapter/js-yaml-adapter.ts b/src/core/js-yaml-adapter.ts similarity index 82% rename from src/adapter/js-yaml-adapter.ts rename to src/core/js-yaml-adapter.ts index 7008821..35ebb7f 100644 --- a/src/adapter/js-yaml-adapter.ts +++ b/src/core/js-yaml-adapter.ts @@ -1,15 +1,8 @@ import { dump, load } from "js-yaml" -import { Settings } from "../settings" -import { SortUtil } from "../util/sort-util" -import { removeTrailingCharacters, splitDocuments } from "../util/yaml-text" - -/** - * @param value Any loaded yaml value. - * @returns True for a mapping without keys, e.g. an empty document. - */ -const isEmptyMapping = (value: unknown): boolean => - typeof value === "object" && value !== null && !Array.isArray(value) && Object.keys(value).length === 0 +import { Settings } from "./settings" +import { SortOrder } from "./sort-order" +import { removeTrailingCharacters, splitDocuments } from "./yaml-text" export class JsYamlAdapter { settings: Settings @@ -41,7 +34,7 @@ export class JsYamlAdapter { /** * Dumps a whole yaml document with the user specific settings. - * An empty document (no content or only comments) is dumped as an empty string. + * An empty document (no content or only comments) is dumped as an empty string, an empty mapping as `{}`. * @param text Loaded yaml document which should be dumped. * @param sortKeys If set to true, the function will sort the keys in the document. * @param custom 0 for the default sort, 1 to 3 for the custom sort keywords. @@ -49,7 +42,7 @@ export class JsYamlAdapter { */ dumpYaml(text: unknown, sortKeys: boolean, custom: number): string { // js-yaml loads an empty document or one with only comments as null or undefined - if (text === undefined || text === null || isEmptyMapping(text)) { + if (text === undefined || text === null) { return "" } return this.dumpValue(text, sortKeys, custom) @@ -87,12 +80,12 @@ export class JsYamlAdapter { */ getKeyComparator(sortKeys: boolean, custom: number): boolean | ((a: string, b: string) => number) { if (custom > 0 && this.settings.useCustomSortRecursively) { - const sort = new SortUtil(this.settings, custom) + const sort = new SortOrder(this.settings, custom) return (a: string, b: string) => sort.customSort(a, b) } if (sortKeys && this.settings.sortOrderReverse) { // mirror js-yaml's default order, which compares by code point - return (a: string, b: string) => SortUtil.compare(b, a) + return (a: string, b: string) => SortOrder.compare(b, a) } return sortKeys } diff --git a/src/processor/array-processor.ts b/src/core/processor/array-processor.ts similarity index 100% rename from src/processor/array-processor.ts rename to src/core/processor/array-processor.ts diff --git a/src/processor/block-processor.ts b/src/core/processor/block-processor.ts similarity index 100% rename from src/processor/block-processor.ts rename to src/core/processor/block-processor.ts diff --git a/src/processor/comment-processor.ts b/src/core/processor/comment-processor.ts similarity index 73% rename from src/processor/comment-processor.ts rename to src/core/processor/comment-processor.ts index e7b3027..0ec9117 100644 --- a/src/processor/comment-processor.ts +++ b/src/core/processor/comment-processor.ts @@ -1,3 +1,8 @@ +import { getInlineComment, isLineComment } from "./yaml-comment" + +/** Anchor of a comment in the last line, which has no following line to stay above. */ +export const LAST_LINE = "vscode-yaml-sort.lastLine" + export class CommentProcessor { store: string[][] = [] inlineStore: string[][] = [] @@ -9,6 +14,13 @@ export class CommentProcessor { this.lines = this.text.split("\n") } + /** + * Remembers the comments. The text itself stays unchanged, js-yaml drops the comments. + */ + preprocess() { + this.findComments() + } + /** * Finds all comments in a given yaml. Stores full line comments with their following line * and inline comments with the code they belong to. @@ -16,7 +28,7 @@ export class CommentProcessor { */ findComments() { this.lines.forEach((line, index) => { - if (CommentProcessor.isLineComment(line)) { + if (isLineComment(line)) { this.addLineToComments(index) } else { this.addInlineCommentToStore(line) @@ -24,43 +36,22 @@ export class CommentProcessor { }) } - static isLineComment(line: string) { - return /^ *#/.test(line) - } - addLineToComments(index: number) { if (index < this.lines.length-1) { this.store.push([this.lines[index], this.lines[index+1]]) } else { - this.store.push([this.lines[index], "vscode-yaml-sort.lastLine"]) + this.store.push([this.lines[index], LAST_LINE]) } } addInlineCommentToStore(line: string) { - const comment = CommentProcessor.getInlineComment(line) + const comment = getInlineComment(line) if (comment !== undefined) { this.inlineStore.push([` ${comment}`, line.slice(0, line.length - comment.length).trimEnd()]) } } - static getInlineComment(line: string): string | undefined { - let inDoubleQuotes = false - let inSingleQuotes = false - - for (let i = 0; i < line.length; i++) { - if (line[i] === "\"" && !inSingleQuotes) { - inDoubleQuotes = !inDoubleQuotes - } else if (line[i] === "'" && !inDoubleQuotes) { - inSingleQuotes = !inSingleQuotes - } else if (line[i] === "#" && i > 0 && (line[i - 1] === " " || line[i - 1] === "\t") && !inDoubleQuotes && !inSingleQuotes) { - return line.slice(i) - } - } - - return undefined - } - postprocess(): string { this.reverseComments() this.store.forEach(comment => { @@ -106,7 +97,7 @@ export class CommentProcessor { } applyComment(comment: string[]) { - if (comment[1] === "vscode-yaml-sort.lastLine") { + if (comment[1] === LAST_LINE) { this.append(comment[0]) } else { this.insert(comment) @@ -139,7 +130,7 @@ export class CommentProcessor { } getIndexOfString(text: string): number { - if (text === "vscode-yaml-sort.lastLine") { + if (text === LAST_LINE) { return this.text.length } else { return this.search(text) @@ -160,18 +151,15 @@ export class CommentProcessor { return this.searchFuzzyForKeyword(text) } - searchExactMatch(text: string): number { - // an empty anchor keeps its previous behavior - if (text === "") { - return this.text.lastIndexOf(text) - } - // comments must be inserted above the exact matching line (see #221). - // a substring search would also find `mysql5:` inside a deeper indented - // line like ` mysql5:` of a `depends_on` block + /** + * @param matches Condition for a line. + * @returns The offset of the last line of the text that meets the condition, or -1. + */ + findLastLine(matches: (line: string) => boolean): number { let result = -1 let offset = 0 for (const line of this.text.split("\n")) { - if (line === text) { + if (matches(line)) { result = offset } offset += line.length + 1 @@ -179,25 +167,38 @@ export class CommentProcessor { return result } + searchExactMatch(text: string): number { + // an empty anchor keeps its previous behavior + if (text === "") { + return this.text.lastIndexOf(text) + } + // comments must be inserted above the exact matching line (see #221). + // a substring search would also find `mysql5:` inside a deeper indented + // line like ` mysql5:` of a `depends_on` block + return this.findLastLine((line) => line === text) + } + searchFuzzyForTrimmedText(text: string): number { // like searchExactMatch(), but the line may have more or less // surrounding whitespaces than the anchor (see #139) const trimmedText = text.trim() - let result = -1 - let offset = 0 - for (const line of this.text.split("\n")) { - if (line.trim() === trimmedText) { - result = offset - } - offset += line.length + 1 - } - return result + return this.findLastLine((line) => line.trim() === trimmedText) } searchFuzzyForKeyword(text: string): number { - // the value of a line may change while sorting, - // so only search for the keyword in front of the first colon - return this.text.lastIndexOf(text.split(":")[0]) + // the value of a line may change while sorting, e.g. quotes are removed, + // so search for the line with the same key in front of the first colon + const key = text.split(":")[0] + const sameKey = this.findLastLine((line) => line.startsWith(`${key}:`)) + if (CommentProcessor.isCommentFound(sameKey)) { + return sameKey + } + const sameKeyIndented = this.findLastLine((line) => line.trim().startsWith(`${key.trim()}:`)) + if (CommentProcessor.isCommentFound(sameKeyIndented)) { + return sameKeyIndented + } + // last resort, e.g. for a key that js-yaml quotes: rather misplace a comment than lose it + return this.text.lastIndexOf(key) } static isCommentFound(index: number): boolean { diff --git a/src/processor/generic-processor.ts b/src/core/processor/generic-processor.ts similarity index 100% rename from src/processor/generic-processor.ts rename to src/core/processor/generic-processor.ts diff --git a/src/processor/helm-processor.ts b/src/core/processor/helm-processor.ts similarity index 100% rename from src/processor/helm-processor.ts rename to src/core/processor/helm-processor.ts diff --git a/src/core/processor/octal-processor.ts b/src/core/processor/octal-processor.ts new file mode 100644 index 0000000..13a8f97 --- /dev/null +++ b/src/core/processor/octal-processor.ts @@ -0,0 +1,9 @@ +import { GenericProcessor } from "./generic-processor" + +export class OctalProcessor extends GenericProcessor { + constructor(text: string) { + // js-yaml loads a value like 0755 as the decimal number 755, which drops the leading zero of a file mode. + // match it as value of a key or a list item, optionally followed by a comment + super("octal", /(?<=(?::|^[ \t]*-)[ \t]+)0[0-7]{3}(?=[ \t]*(?:#.*)?$)/gm, text) + } +} diff --git a/src/core/processor/processor-pipeline.ts b/src/core/processor/processor-pipeline.ts new file mode 100644 index 0000000..626a1a7 --- /dev/null +++ b/src/core/processor/processor-pipeline.ts @@ -0,0 +1,75 @@ +import { ArrayProcessor } from "./array-processor" +import { BlockProcessor } from "./block-processor" +import { CommentProcessor } from "./comment-processor" +import { HelmProcessor } from "./helm-processor" +import { OctalProcessor } from "./octal-processor" +import { SpacingProcessor } from "./spacing-processor" +import { TabsToSpacesProcessor } from "./tabs-to-spaces-processor" +import { Settings } from "../settings" + +/** + * A processor that hides parts of a document from js-yaml and restores them afterwards. + */ +interface ReversibleProcessor { + text: string + preprocess(): void + postprocess(): void +} + +/** + * Runs the processors around loading and dumping a document with js-yaml. + * + * `preprocess()` applies the enabled processors in a fixed order, `postprocess()` restores their + * values in the reverse order, so a processor always sees the text the way it left it. + */ +export class ProcessorPipeline { + text: string + settings: Settings + private applied: ReversibleProcessor[] = [] + + constructor(text: string, settings = new Settings()) { + this.text = text + this.settings = settings + } + + /** + * @returns Factories for the enabled processors, in the order of preprocessing. + */ + private enabledProcessors(): ((text: string) => ReversibleProcessor)[] { + const processors: [boolean, (text: string) => ReversibleProcessor][] = [ + [this.settings.useBlockProcessor, (text) => new BlockProcessor(text)], + [this.settings.useArrayProcessor, (text) => new ArrayProcessor(text)], + [this.settings.useHelmProcessor, (text) => new HelmProcessor(text)], + [this.settings.useOctalProcessor, (text) => new OctalProcessor(text)], + [this.settings.useCommentProcessor, (text) => new CommentProcessor(text)], + ] + return processors.filter(([enabled]) => enabled).map(([, create]) => create) + } + + preprocess() { + const tabsToSpaces = new TabsToSpacesProcessor(this.text, this.settings) + tabsToSpaces.preprocess() + this.text = tabsToSpaces.text + // each processor works on the text the previous one left + this.applied = this.enabledProcessors().map((create) => { + const processor = create(this.text) + processor.preprocess() + this.text = processor.text + return processor + }) + } + + postprocess() { + // add empty lines first: the hidden values, e.g. the content of block scalars, must not get + // any, and comments are put directly above their key afterwards + const spacing = new SpacingProcessor(this.text, this.settings) + spacing.postprocess() + this.text = spacing.text + + for (const processor of [...this.applied].reverse()) { + processor.text = this.text + processor.postprocess() + this.text = processor.text + } + } +} diff --git a/src/processor/spacing-processor.ts b/src/core/processor/spacing-processor.ts similarity index 100% rename from src/processor/spacing-processor.ts rename to src/core/processor/spacing-processor.ts diff --git a/src/core/processor/tabs-to-spaces-processor.ts b/src/core/processor/tabs-to-spaces-processor.ts new file mode 100644 index 0000000..ec28b5d --- /dev/null +++ b/src/core/processor/tabs-to-spaces-processor.ts @@ -0,0 +1,20 @@ +import { Settings } from "../settings" + +/** + * Replaces tabs in the indentation with spaces, because yaml does not allow tabs for indentation. + */ +export class TabsToSpacesProcessor { + text: string + settings: Settings + + constructor(text: string, settings = new Settings()) { + this.text = text + this.settings = settings + } + + preprocess() { + const spaces = " ".repeat(this.settings.indent) + // only the indentation: a tab inside a value, e.g. in a quoted string, is part of the value + this.text = this.text.replace(/^[ \t]+/gm, (indentation) => indentation.replace(/\t/g, spaces)) + } +} diff --git a/src/core/processor/yaml-comment.ts b/src/core/processor/yaml-comment.ts new file mode 100644 index 0000000..7757909 --- /dev/null +++ b/src/core/processor/yaml-comment.ts @@ -0,0 +1,45 @@ +/* + * Recognizes comments in single lines of yaml. + */ + +/** + * @param line A line of yaml. + * @returns True if the whole line is a comment. + */ +export const isLineComment = (line: string): boolean => /^ *#/.test(line) + +/** + * A quote only starts a quoted value at the beginning of a value, e.g. after `key: `, `- ` or `[`. + * Elsewhere it is part of a plain value, like the apostrophe in `key: it's`. + */ +const startsValue = (line: string, index: number): boolean => index === 0 || /[\s[{,]/.test(line[index - 1]) + +/** + * Finds the comment at the end of a line, ignoring `#` inside quoted values. + * @param line A line of yaml. + * @returns The comment starting with `#`, or undefined if the line has none. + */ +export const getInlineComment = (line: string): string | undefined => { + let quote: string | undefined + + for (let i = 0; i < line.length; i++) { + const char = line[i] + if (quote !== undefined) { + if (quote === "\"" && char === "\\") { + // skip the escaped character, e.g. \" + i++ + } else if (quote === "'" && char === "'" && line[i + 1] === "'") { + // '' is an escaped single quote + i++ + } else if (char === quote) { + quote = undefined + } + } else if ((char === "\"" || char === "'") && startsValue(line, i)) { + quote = char + } else if (char === "#" && i > 0 && (line[i - 1] === " " || line[i - 1] === "\t")) { + return line.slice(i) + } + } + + return undefined +} diff --git a/src/core/settings.ts b/src/core/settings.ts new file mode 100644 index 0000000..64270b4 --- /dev/null +++ b/src/core/settings.ts @@ -0,0 +1,179 @@ +import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" +import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema" +import { CORE_SCHEMA, DEFAULT_SCHEMA, FAILSAFE_SCHEMA, JSON_SCHEMA, Schema } from "js-yaml" + +export type QuotingType = "'" | "\"" + +// js-yaml only supports these styles for the !!null tag +const NULL_STYLES = ["lowercase", "uppercase", "camelcase", "canonical", "empty"] + +/** + * The values used when the configuration has none. They must match the defaults declared in + * package.json, which a unit test verifies. + */ +export const DEFAULT_CONFIGURATION = { + customSortKeywords_1: ["apiVersion", "kind", "metadata", "spec", "data"], + customSortKeywords_2: [] as string[], + customSortKeywords_3: [] as string[], + emptyLinesUntilLevel: 0, + extensions: ["yaml", "yml"], + forceQuotes: false, + indent: 2, + lineWidth: 500, + locale: "en", + noArrayIndent: false, + noCompatMode: false, + notifySuccess: true, + nullStyle: "lowercase", + quotingType: "'" as QuotingType, + schema: "DEFAULT_SCHEMA", + sortArrays: false, + sortOnSave: 0, + sortOrderReverse: false, + useArrayProcessor: true, + useBlockProcessor: true, + useCommentProcessor: true, + useCustomSortRecursively: false, + useHelmProcessor: true, + useLeadingDashes: true, + useOctalProcessor: true, +} + +export type ConfigurationKey = keyof typeof DEFAULT_CONFIGURATION + +/** + * Read access to the extension's configuration section. VS Code's `WorkspaceConfiguration` + * satisfies it, so the core does not depend on the VS Code API. + */ +export interface ConfigurationReader { + get(key: string): T | undefined +} + +const NO_CONFIGURATION: ConfigurationReader = { get: () => undefined } + +/** + * A snapshot of the extension's settings. + * + * All values are read once, when the snapshot is created. Afterwards they can be changed on the + * instance, e.g. in tests, and every getter reflects the change. + */ +export class Settings { + static readonly SECTION = "vscode-yaml-sort" + + customSortKeywords1: string[] + customSortKeywords2: string[] + customSortKeywords3: string[] + emptyLinesUntilLevel: number + extensions: string[] + forceQuotes: boolean + indent: number + lineWidth: number + locale: string + nullStyle: string + noArrayIndent: boolean + noCompatMode: boolean + notifySuccess: boolean + quotingType: QuotingType + schema: Schema + sortArrays: boolean + sortOnSave: number + sortOrderReverse: boolean + useCustomSortRecursively: boolean + useLeadingDashes: boolean + useArrayProcessor: boolean + useBlockProcessor: boolean + useCommentProcessor: boolean + useHelmProcessor: boolean + useOctalProcessor: boolean + + /** + * @param configuration The extension's configuration section. Missing values fall back to + * DEFAULT_CONFIGURATION, so `new Settings()` yields the defaults. + */ + constructor(configuration: ConfigurationReader = NO_CONFIGURATION) { + const read = (key: K): (typeof DEFAULT_CONFIGURATION)[K] => { + const value = configuration.get<(typeof DEFAULT_CONFIGURATION)[K]>(key) ?? DEFAULT_CONFIGURATION[key] + // copy lists, so changing a snapshot never changes the defaults or another snapshot + return (Array.isArray(value) ? [...value] : value) as (typeof DEFAULT_CONFIGURATION)[K] + } + this.customSortKeywords1 = read("customSortKeywords_1") + this.customSortKeywords2 = read("customSortKeywords_2") + this.customSortKeywords3 = read("customSortKeywords_3") + this.emptyLinesUntilLevel = read("emptyLinesUntilLevel") + this.extensions = read("extensions") + this.forceQuotes = read("forceQuotes") + this.indent = read("indent") + this.lineWidth = read("lineWidth") + this.locale = read("locale") + this.nullStyle = read("nullStyle") + this.noArrayIndent = read("noArrayIndent") + this.noCompatMode = read("noCompatMode") + this.notifySuccess = read("notifySuccess") + this.quotingType = read("quotingType") + this.schema = Settings.getJsYamlSchemaFromString(read("schema")) + this.sortArrays = read("sortArrays") + this.sortOnSave = read("sortOnSave") + this.sortOrderReverse = read("sortOrderReverse") + this.useCustomSortRecursively = read("useCustomSortRecursively") + this.useLeadingDashes = read("useLeadingDashes") + this.useArrayProcessor = read("useArrayProcessor") + this.useBlockProcessor = read("useBlockProcessor") + this.useCommentProcessor = read("useCommentProcessor") + this.useHelmProcessor = read("useHelmProcessor") + this.useOctalProcessor = read("useOctalProcessor") + } + + /** + * @param index 1, 2 or 3. + * @returns The keywords of the custom sort, or an empty list for any other index. + */ + getCustomSortKeywords(index: number): string[] { + switch (index) { + case 1: + return this.customSortKeywords1 + case 2: + return this.customSortKeywords2 + case 3: + return this.customSortKeywords3 + default: + return [] + } + } + + getExtensions(): string[] { + return this.extensions + } + + getQuotingType(): QuotingType { + return this.quotingType + } + + getSchema(): Schema { + return this.schema + } + + getNullStyle(): string { + return NULL_STYLES.includes(this.nullStyle) ? this.nullStyle : "lowercase" + } + + static getJsYamlSchemaFromString(schema: string): Schema { + switch (schema) { + case "HOMEASSISTANT_SCHEMA": + return HOMEASSISTANT_SCHEMA as Schema + case "CLOUDFORMATION_SCHEMA": + return CLOUDFORMATION_SCHEMA as Schema + case "CORE_SCHEMA": + return CORE_SCHEMA + case "FAILSAFE_SCHEMA": + return FAILSAFE_SCHEMA + case "JSON_SCHEMA": + return JSON_SCHEMA + default: + return DEFAULT_SCHEMA + } + } + + doSortOnSave(): boolean { + return (this.sortOnSave >= 0 && this.sortOnSave <= 3) + } +} diff --git a/src/util/sort-util.ts b/src/core/sort-order.ts similarity index 92% rename from src/util/sort-util.ts rename to src/core/sort-order.ts index a061447..c0ea523 100644 --- a/src/util/sort-util.ts +++ b/src/core/sort-order.ts @@ -1,6 +1,6 @@ -import { Settings } from "../settings" +import { Settings } from "./settings" -export class SortUtil { +export class SortOrder { settings: Settings custom = 0 @@ -16,7 +16,7 @@ export class SortUtil { const indexB = sortOrder.indexOf(b) if (indexA > -1 && indexB > -1) { - return SortUtil.compare(indexA, indexB) + return SortOrder.compare(indexA, indexB) } if (indexA !== -1 && indexB === -1) { return -1 @@ -76,7 +76,7 @@ const compareArrayItems = (a: unknown, b: unknown): number => { // Array.prototype.sort is stable, so these items keep their order return 0 } - return SortUtil.compare(a as number | string, b as number | string) + return SortOrder.compare(a as number | string, b as number | string) } /** diff --git a/src/core/yaml-file-finder.ts b/src/core/yaml-file-finder.ts new file mode 100644 index 0000000..0aa96be --- /dev/null +++ b/src/core/yaml-file-finder.ts @@ -0,0 +1,31 @@ +import { join } from "path" + +import { sync } from "glob" + +import { Settings } from "./settings" + +/** Folders whose files belong to other projects or tools and must never be rewritten. */ +const IGNORED_FOLDERS = ["**/node_modules/**", "**/.git/**"] + +export class YamlFileFinder { + settings: Settings + + constructor(settings = new Settings()) { + this.settings = settings + } + + /** + * Finds all files with one of the configured extensions in a directory and its subdirectories, + * except for installed dependencies (node_modules) and git internals. + * @param path Directory to search. + * @returns Paths of the files, starting with the given directory. Each file is listed once. + */ + getFiles(path: string): string[] { + const extensions = [...new Set(this.settings.getExtensions())] + // the directory is passed as cwd instead of being part of the pattern: glob treats a backslash + // in a pattern as an escape character, so a windows path like C:\project would match nothing + return extensions.flatMap((extension) => + sync(`**/*.${extension}`, { cwd: path, dot: true, ignore: IGNORED_FOLDERS }).map((file) => join(path, file)) + ) + } +} diff --git a/src/core/yaml-sorter.ts b/src/core/yaml-sorter.ts new file mode 100644 index 0000000..03783e9 --- /dev/null +++ b/src/core/yaml-sorter.ts @@ -0,0 +1,100 @@ +import { JsYamlAdapter } from "./js-yaml-adapter" +import { ProcessorPipeline } from "./processor/processor-pipeline" +import { Settings } from "./settings" +import { sortArraysRecursively } from "./sort-order" + +/** + * @param value Any loaded yaml value. + * @returns True for a mapping, i.e. an object that is no list. + */ +const isMapping = (value: unknown): value is Record => + typeof value === "object" && value !== null && !Array.isArray(value) + +export class YamlSorter { + settings: Settings + jsYamlAdapter: JsYamlAdapter + + constructor(settings = new Settings()) { + this.settings = settings + this.jsYamlAdapter = new JsYamlAdapter(settings) + } + + static getNumberOfLeadingSpaces(text: string): number { + if (!text.startsWith(" ")) { + return 0 + } else { + return text.search(/\S/) + } + } + + /** + * Sorts a single yaml document. + * @param unsortedYaml The content of one document, without delimiter. + * @param customSort 0 for the default sort, 1 to 3 for the custom sort keywords. + * @returns The sorted document. + * @throws YAMLException if the document is not valid yaml. + */ + sortYaml(unsortedYaml: string, customSort = 0): string { + const processor = new ProcessorPipeline(unsortedYaml, this.settings) + processor.preprocess() + + const doc = this.jsYamlAdapter.load(processor.text) + if (this.settings.sortArrays) { + sortArraysRecursively(doc) + } + + processor.text = this.dumpSorted(doc, customSort) + processor.postprocess() + return processor.text + } + + /** + * Dumps a loaded document with sorted keys. A custom sort that is not applied recursively puts + * the custom sort keywords on top and sorts the remaining keys as usual. + * @param doc The loaded document. + * @param customSort 0 for the default sort, 1 to 3 for the custom sort keywords. + * @returns The dumped document. + */ + dumpSorted(doc: unknown, customSort: number): string { + if (customSort === 0 || this.settings.useCustomSortRecursively || !isMapping(doc)) { + return this.jsYamlAdapter.dumpYaml(doc, true, customSort) + } + // check for the key itself, values like 0, false or null must be ordered too + const keywords = this.settings.getCustomSortKeywords(customSort) + .filter((key) => Object.prototype.hasOwnProperty.call(doc, key)) + // build the remaining keys anew instead of deleting them from the loaded document + const remainder = Object.fromEntries(Object.entries(doc).filter(([key]) => !keywords.includes(key))) + const parts = keywords.map((key) => this.dumpKeyword(key, doc[key], customSort)) + if (Object.keys(remainder).length > 0) { + parts.push(this.jsYamlAdapter.dumpYaml(remainder, true, customSort)) + } + return parts.join("\n") + } + + /** + * Dumps one top-level keyword of a custom sort together with its value. js-yaml dumps both, so + * the keyword is quoted if necessary and the value keeps its usual style. + * @param key The keyword. + * @param value Its value. + * @param customSort 1 to 3, the custom sort that is applied. + * @returns The yaml lines for the keyword, without trailing line break. + */ + dumpKeyword(key: string, value: unknown, customSort: number): string { + return this.jsYamlAdapter.dumpValue({ [key]: value }, true, customSort) + } + + /** + * Formats a single yaml document without sorting it. + * @param yaml The content of one document, without delimiter. + * @param useLeadingDashes Whether the result should start with a delimiter. + * @returns The formatted document. + * @throws YAMLException if the document is not valid yaml. + */ + formatYaml(yaml: string, useLeadingDashes: boolean): string { + const processor = new ProcessorPipeline(yaml, this.settings) + processor.preprocess() + processor.text = this.jsYamlAdapter.dumpYaml(this.jsYamlAdapter.load(processor.text), false, 0) + processor.postprocess() + return useLeadingDashes ? `---\n${processor.text}` : processor.text + } +} diff --git a/src/util/yaml-text.ts b/src/core/yaml-text.ts similarity index 100% rename from src/util/yaml-text.ts rename to src/core/yaml-text.ts diff --git a/src/extension.ts b/src/extension.ts index 7814445..72495b0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,31 +1,34 @@ -import { ExtensionContext, Uri, commands } from "vscode" +import { ExtensionContext, Uri, commands, window } from "vscode" -import { VsCodeAdapter } from "./adapter/vs-code-adapter" -import { Controller } from "./controller/controller" -import { sortYamlFilesInDirectory } from "./controller/file-sorter" -import { YamlFormattingProvider } from "./controller/formatting-provider" +import { sortYamlFilesInDirectory } from "./vscode/directory-sorter" +import { EditorCommands } from "./vscode/editor-commands" +import { YamlFormattingProvider } from "./vscode/formatting-provider" +import { VsCodeAdapter } from "./vscode/vs-code-adapter" + +/** + * Runs a command on the active editor, or explains why it cannot run, e.g. when the command is + * started with a key binding while no editor is open. + */ +const withActiveEditor = (command: (editorCommands: EditorCommands) => T) => (): T | undefined => { + const editor = window.activeTextEditor + if (!editor) { + window.showErrorMessage("Open a yaml file to run this command.") + return undefined + } + return command(new EditorCommands(editor)) +} export function activate(context: ExtensionContext) { context.subscriptions.push( VsCodeAdapter.registerFormatter(new YamlFormattingProvider()), - commands.registerCommand("vscode-yaml-sort.sortYaml", () => { - new Controller().sortYamlWrapper() - }), - commands.registerCommand("vscode-yaml-sort.validateYaml", () => { - new Controller().validateYamlWrapper() - }), - commands.registerCommand("vscode-yaml-sort.formatYaml", () => { - new Controller().formatYamlWrapper() - }), - commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri?: Uri) => { - sortYamlFilesInDirectory(uri) - }) + commands.registerCommand("vscode-yaml-sort.sortYaml", withActiveEditor((editor) => editor.sortYamlWrapper())), + commands.registerCommand("vscode-yaml-sort.validateYaml", withActiveEditor((editor) => editor.validateYamlWrapper())), + commands.registerCommand("vscode-yaml-sort.formatYaml", withActiveEditor((editor) => editor.formatYamlWrapper())), + commands.registerCommand("vscode-yaml-sort.sortYamlFilesInDirectory", (uri?: Uri) => sortYamlFilesInDirectory(uri)) ) for (const index of [1, 2, 3]) { context.subscriptions.push( - commands.registerCommand(`vscode-yaml-sort.customSortYaml_${index}`, () => { - new Controller().sortYamlWrapper(index) - }) + commands.registerCommand(`vscode-yaml-sort.customSortYaml_${index}`, withActiveEditor((editor) => editor.sortYamlWrapper(index))) ) } } diff --git a/src/processor/octal-processor.ts b/src/processor/octal-processor.ts deleted file mode 100644 index fc498a4..0000000 --- a/src/processor/octal-processor.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { GenericProcessor } from "./generic-processor" - -export class OctalProcessor extends GenericProcessor { - constructor(text: string) { - super("octal", /(?<=:\s)(0[0-7]{3})$/gm, text) - } -} diff --git a/src/processor/tabs-to-spaces-processor.ts b/src/processor/tabs-to-spaces-processor.ts deleted file mode 100644 index 4c78918..0000000 --- a/src/processor/tabs-to-spaces-processor.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Settings } from "../settings" - -export class TabsToSpacesProcessor { - text: string - settings: Settings - - constructor(text: string, settings = new Settings()) { - this.text = text - this.settings = settings - } - - preprocess() { - const spaces = " ".repeat(this.settings.indent) - this.text = this.text.replace(/\t/mg, spaces) - } -} - diff --git a/src/settings.ts b/src/settings.ts deleted file mode 100644 index 18dfbea..0000000 --- a/src/settings.ts +++ /dev/null @@ -1,145 +0,0 @@ -import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" -import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema" -import { CORE_SCHEMA, DEFAULT_SCHEMA, FAILSAFE_SCHEMA, JSON_SCHEMA, Schema } from "js-yaml" -import { ConfigurationScope, WorkspaceConfiguration, workspace } from "vscode" - -type QuotingType = "'" | "\"" - -// js-yaml only supports these styles for the !!null tag -const NULL_STYLES = ["lowercase", "uppercase", "camelcase", "canonical", "empty"] - -/** - * A snapshot of the extension's settings. - * - * All values are read once, when the snapshot is created. Afterwards they can be changed on the - * instance, e.g. in tests, and every getter reflects the change. - */ -export class Settings { - static readonly SECTION = "vscode-yaml-sort" - - customSortKeywords1: string[] - customSortKeywords2: string[] - customSortKeywords3: string[] - emptyLinesUntilLevel: number - extensions: string[] - forceQuotes: boolean - indent: number - lineWidth: number - locale: string - nullStyle: string - noArrayIndent: boolean - noCompatMode: boolean - notifySuccess: boolean - quotingType: QuotingType - schema: Schema - sortArrays: boolean - sortOnSave: number - sortOrderReverse: boolean - useCustomSortRecursively: boolean - useLeadingDashes: boolean - useArrayProcessor: boolean - useBlockProcessor: boolean - useCommentProcessor: boolean - useHelmProcessor: boolean - useOctalProcessor: boolean - - /** - * @param configuration The extension's configuration section. Defaults to the global and workspace settings. - */ - constructor(configuration: WorkspaceConfiguration = workspace.getConfiguration(Settings.SECTION)) { - const read = (name: string) => configuration.get(name) as T - this.customSortKeywords1 = read("customSortKeywords_1") - this.customSortKeywords2 = read("customSortKeywords_2") - this.customSortKeywords3 = read("customSortKeywords_3") - this.emptyLinesUntilLevel = read("emptyLinesUntilLevel") - this.extensions = read("extensions") - this.forceQuotes = read("forceQuotes") - this.indent = read("indent") - this.lineWidth = read("lineWidth") - this.locale = read("locale") - this.nullStyle = read("nullStyle") - this.noArrayIndent = read("noArrayIndent") - this.noCompatMode = read("noCompatMode") - this.notifySuccess = read("notifySuccess") - this.quotingType = read("quotingType") - this.schema = Settings.getJsYamlSchemaFromString(read("schema")) - this.sortArrays = read("sortArrays") - this.sortOnSave = read("sortOnSave") - this.sortOrderReverse = read("sortOrderReverse") - this.useCustomSortRecursively = read("useCustomSortRecursively") - this.useLeadingDashes = read("useLeadingDashes") - this.useArrayProcessor = read("useArrayProcessor") - this.useBlockProcessor = read("useBlockProcessor") - this.useCommentProcessor = read("useCommentProcessor") - this.useHelmProcessor = read("useHelmProcessor") - this.useOctalProcessor = read("useOctalProcessor") - } - - /** - * Reads the settings that apply to a document: workspace folder settings in a multi-root - * workspace and language specific settings like `"[yaml]": { "vscode-yaml-sort.indent": 4 }`. - * @param document The document, or its uri and language id. - * @param getConfiguration Injectable for tests. - * @returns The settings for the document. - */ - static forDocument( - document: ConfigurationScope, - getConfiguration: (section?: string, scope?: ConfigurationScope) => WorkspaceConfiguration = workspace.getConfiguration - ): Settings { - return new Settings(getConfiguration(Settings.SECTION, document)) - } - - /** - * @param index 1, 2 or 3. - * @returns The keywords of the custom sort, or an empty list for any other index. - */ - getCustomSortKeywords(index: number): string[] { - switch (index) { - case 1: - return this.customSortKeywords1 - case 2: - return this.customSortKeywords2 - case 3: - return this.customSortKeywords3 - default: - return [] - } - } - - getExtensions(): string[] { - return this.extensions - } - - getQuotingType(): QuotingType { - return this.quotingType - } - - getSchema(): Schema { - return this.schema - } - - getNullStyle(): string { - return NULL_STYLES.includes(this.nullStyle) ? this.nullStyle : "lowercase" - } - - static getJsYamlSchemaFromString(schema: string): Schema { - switch (schema) { - case "HOMEASSISTANT_SCHEMA": - return HOMEASSISTANT_SCHEMA as Schema - case "CLOUDFORMATION_SCHEMA": - return CLOUDFORMATION_SCHEMA as Schema - case "CORE_SCHEMA": - return CORE_SCHEMA - case "FAILSAFE_SCHEMA": - return FAILSAFE_SCHEMA - case "JSON_SCHEMA": - return JSON_SCHEMA - default: - return DEFAULT_SCHEMA - } - } - - doSortOnSave(): boolean { - return (this.sortOnSave >= 0 && this.sortOnSave <= 3) - } -} diff --git a/src/test/suite/util/resources/bar.yml b/src/test/files/file-finder/bar.yml similarity index 100% rename from src/test/suite/util/resources/bar.yml rename to src/test/files/file-finder/bar.yml diff --git a/src/test/suite/util/resources/foo.yaml b/src/test/files/file-finder/foo.yaml similarity index 100% rename from src/test/suite/util/resources/foo.yaml rename to src/test/files/file-finder/foo.yaml diff --git a/src/test/suite/util/resources/readFile.txt b/src/test/files/file-finder/readFile.txt similarity index 100% rename from src/test/suite/util/resources/readFile.txt rename to src/test/files/file-finder/readFile.txt diff --git a/src/test/suite/util/resources/subfolder/.customyaml b/src/test/files/file-finder/subfolder/.customyaml similarity index 100% rename from src/test/suite/util/resources/subfolder/.customyaml rename to src/test/files/file-finder/subfolder/.customyaml diff --git a/src/test/suite/util/resources/subfolder/foo2.yaml b/src/test/files/file-finder/subfolder/foo2.yaml similarity index 100% rename from src/test/suite/util/resources/subfolder/foo2.yaml rename to src/test/files/file-finder/subfolder/foo2.yaml diff --git a/src/test/suite/util/resources/subfolder/text.txt b/src/test/files/file-finder/subfolder/text.txt similarity index 100% rename from src/test/suite/util/resources/subfolder/text.txt rename to src/test/files/file-finder/subfolder/text.txt diff --git a/src/test/integration/configuration.test.ts b/src/test/integration/configuration.test.ts new file mode 100644 index 0000000..39ae411 --- /dev/null +++ b/src/test/integration/configuration.test.ts @@ -0,0 +1,24 @@ +import { deepEqual, equal } from "assert" + +import { Uri, workspace } from "vscode" + +import { readSettings } from "../../vscode/configuration" + +suite("Test readSettings()", () => { + test("should read the configuration for the given document", () => { + const document = { uri: Uri.file("/project/a.yaml"), languageId: "yaml" } + const calls: unknown[][] = [] + const getConfiguration = (section?: string, scope?: unknown) => { + calls.push([section, scope]) + return workspace.getConfiguration(section) + } + + readSettings(document, getConfiguration) + + deepEqual(calls, [["vscode-yaml-sort", document]]) + }) + + test("should read the values of VS Code's configuration", () => { + equal(readSettings().indent, workspace.getConfiguration("vscode-yaml-sort").get("indent")) + }) +}) diff --git a/src/test/suite/controller/controller.test.ts b/src/test/integration/editor-commands.test.ts similarity index 67% rename from src/test/suite/controller/controller.test.ts rename to src/test/integration/editor-commands.test.ts index dcc5ed2..c892d0e 100644 --- a/src/test/suite/controller/controller.test.ts +++ b/src/test/integration/editor-commands.test.ts @@ -2,9 +2,9 @@ import { equal } from "assert" import { readFileSync, writeFileSync } from "fs" import { resolve } from "path" -import { Uri, commands, window, workspace } from "vscode" +import { Selection, Uri, commands, window, workspace } from "vscode" -import { Controller } from "../../../controller/controller" +import { EditorCommands } from "../../vscode/editor-commands" suite("Test sortYamlFiles", () => { @@ -30,7 +30,7 @@ suite("Test validateYamlWrapper", () => { const uri = Uri.parse(resolve("./src/test/files/testValidateYamlWrapper.yaml")) const doc = await workspace.openTextDocument(uri) await window.showTextDocument(doc, { preview: false }) - equal(new Controller().validateYamlWrapper(), true) + equal(new EditorCommands().validateYamlWrapper(), true) }) }) @@ -47,7 +47,7 @@ suite("Test formatYamlWrapper", () => { "key:\n" + " key2: value" - equal(new Controller().formatYamlWrapper()[0].newText, expected) + equal((await new EditorCommands().formatYamlWrapper())[0].newText, expected) } else { equal(true, false) } @@ -65,7 +65,7 @@ suite("Test formatYamlWrapper", () => { "---\n" + "key2: value" - equal(new Controller().formatYamlWrapper()[0].newText, expected) + equal((await new EditorCommands().formatYamlWrapper())[0].newText, expected) } else { equal(true, false) } @@ -82,4 +82,26 @@ suite("Test formatYamlWrapper", () => { equal(true, false) } }) -}) \ No newline at end of file +}) + +suite("Test formatYamlWrapper with a selection", () => { + test("should format the whole document instead of replacing the selection with it", async () => { + const document = await workspace.openTextDocument({ language: "yaml", content: "b: 1\na: 2" }) + const editor = await window.showTextDocument(document, { preview: false }) + editor.selection = new Selection(0, 0, 0, 5) + + await new EditorCommands(editor).formatYamlWrapper() + + equal(document.getText(), "---\nb: 1\na: 2") + }) +}) + +suite("Test editor commands without an active editor", () => { + test("should not fail when no editor is open", async () => { + await commands.executeCommand("workbench.action.closeAllEditors") + + await commands.executeCommand("vscode-yaml-sort.sortYaml") + await commands.executeCommand("vscode-yaml-sort.formatYaml") + await commands.executeCommand("vscode-yaml-sort.validateYaml") + }) +}) diff --git a/src/test/integration/error-reporter.test.ts b/src/test/integration/error-reporter.test.ts new file mode 100644 index 0000000..d588807 --- /dev/null +++ b/src/test/integration/error-reporter.test.ts @@ -0,0 +1,26 @@ +import { deepEqual } from "assert" + +import { spy } from "sinon" + +import { ErrorReporter } from "../../vscode/error-reporter" +import { Severity } from "../../vscode/vs-code-adapter" + +suite("Test ErrorReporter - handleError()", () => { + test("should show the message of an error", () => { + const errorReporter = new ErrorReporter() + const showMessage = spy(errorReporter.vsCodeAdapter, "showMessage") + + errorReporter.handleError(new Error("broken")) + + deepEqual(showMessage.args, [[Severity.ERROR, "broken"]]) + }) + + test("should show a thrown value that is no error instead of swallowing it", () => { + const errorReporter = new ErrorReporter() + const showMessage = spy(errorReporter.vsCodeAdapter, "showMessage") + + errorReporter.handleError("no error") + + deepEqual(showMessage.args, [[Severity.ERROR, "no error"]]) + }) +}) diff --git a/src/test/suite/extension.test.ts b/src/test/integration/extension.test.ts similarity index 76% rename from src/test/suite/extension.test.ts rename to src/test/integration/extension.test.ts index 8a4e757..de86998 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/integration/extension.test.ts @@ -3,29 +3,7 @@ import { resolve } from "path" import { Position, Range, Selection, Uri, commands, window, workspace } from "vscode" -import { Controller } from "../../controller/controller" -import { Settings } from "../../settings" - -suite("Test getCustomSortKeywords", () => { - test("should return values of `vscode-yaml-sort.customSortKeywords_1`", () => { - const settings = new Settings() - settings.getCustomSortKeywords = function () { - return ["apiVersion", "kind", "metadata", "spec", "data"] - } - deepEqual(settings.getCustomSortKeywords(1), ["apiVersion", "kind", "metadata", "spec", "data"]) - }) - test("should return `[]` for custom keywords 2 and 3", () => { - deepEqual(new Settings().getCustomSortKeywords(2), []) - deepEqual(new Settings().getCustomSortKeywords(3), []) - }) - - test("should return [] when parameter is not in [1, 2, 3]", () => { - deepEqual(new Settings().getCustomSortKeywords(0), []) - deepEqual(new Settings().getCustomSortKeywords(4), []) - deepEqual(new Settings().getCustomSortKeywords(-1), []) - deepEqual(new Settings().getCustomSortKeywords(1.5), []) - }) -}) +import { EditorCommands } from "../../vscode/editor-commands" suite("Test validateYaml", () => { test("do not fail when executing command", async () => { @@ -83,7 +61,7 @@ suite("Test sortYamlWrapper", () => { new Position(activeEditor.document.lineCount + 1, 0)), actual)) - notDeepEqual(new Controller().sortYamlWrapper(), []) + notDeepEqual(await new EditorCommands().sortYamlWrapper(), []) } else { equal(true, false) } @@ -107,7 +85,7 @@ suite("Test sortYamlWrapper", () => { actual)) activeEditor.selection = new Selection(0, 0, 0, 4) - deepEqual(new Controller().sortYamlWrapper(), []) + deepEqual(await new EditorCommands().sortYamlWrapper(), []) } else { equal(true, false) } diff --git a/src/test/suite/controller/formatting-provider.test.ts b/src/test/integration/formatting-provider.test.ts similarity index 96% rename from src/test/suite/controller/formatting-provider.test.ts rename to src/test/integration/formatting-provider.test.ts index 2c877ca..14d7bfe 100644 --- a/src/test/suite/controller/formatting-provider.test.ts +++ b/src/test/integration/formatting-provider.test.ts @@ -2,7 +2,7 @@ import { deepEqual, equal } from "assert" import { window, workspace } from "vscode" -import { YamlFormattingProvider } from "../../../controller/formatting-provider" +import { YamlFormattingProvider } from "../../vscode/formatting-provider" suite("Test YamlFormattingProvider - provideDocumentFormattingEdits()", () => { const provider = new YamlFormattingProvider() diff --git a/src/test/suite/index.ts b/src/test/integration/index.ts similarity index 100% rename from src/test/suite/index.ts rename to src/test/integration/index.ts diff --git a/src/test/integration/vs-code-adapter.test.ts b/src/test/integration/vs-code-adapter.test.ts new file mode 100644 index 0000000..f3fff16 --- /dev/null +++ b/src/test/integration/vs-code-adapter.test.ts @@ -0,0 +1,46 @@ +import { equal } from "assert" + +import { spy } from "sinon" +import { window, workspace } from "vscode" + +import { Severity, VsCodeAdapter } from "../../vscode/vs-code-adapter" + +suite("Test VsCodeAdapter - getFullDocumentRange()", () => { + test("should cover the whole document", async () => { + const document = await workspace.openTextDocument({ language: "yaml", content: "a: 1\nb: 2\n" }) + + equal(document.getText(VsCodeAdapter.getFullDocumentRange(document)), "a: 1\nb: 2\n") + }) +}) + +suite("Test VsCodeAdapter - showMessage()", () => { + const vsCodeAdapter = new VsCodeAdapter() + test("when Severity.ERROR should show error message", () => { + const spyError = spy(window, "showErrorMessage") + equal(spyError.called, false) + vsCodeAdapter.showMessage(Severity.ERROR, "Test message") + equal(spyError.called, true) + }) + + test("when Severity.INFO should show info message", () => { + const spyNotify = spy(vsCodeAdapter, "notify") + equal(spyNotify.called, false) + vsCodeAdapter.showMessage(Severity.INFO, "Test message") + equal(spyNotify.called, true) + }) +}) + +suite("Test VsCodeAdapter - notify()", () => { + test("when notifySuccess should show info message", () => { + const vsCodeAdapter = new VsCodeAdapter() + const spyObject = spy(window, "showInformationMessage") + + vsCodeAdapter.settings.notifySuccess = false + vsCodeAdapter.notify("Test message") + equal(spyObject.called, false) + + vsCodeAdapter.settings.notifySuccess = true + vsCodeAdapter.notify("Test message") + equal(spyObject.called, true) + }) +}) diff --git a/src/test/runTest.ts b/src/test/runTest.ts index 59d459d..f1db7ad 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -2,24 +2,22 @@ import { resolve } from "path" import { runTests } from "@vscode/test-electron" -async function main() { - try { - // The folder containing the Extension Manifest package.json - // Passed to `--extensionDevelopmentPath` - const extensionDevelopmentPath = resolve(__dirname, "../../") +const main = async (): Promise => { + // The folder containing the Extension Manifest package.json + // Passed to `--extensionDevelopmentPath` + const extensionDevelopmentPath = resolve(__dirname, "../../") - // The path to test runner - // Passed to --extensionTestsPath - const extensionTestsPath = resolve(__dirname, "./suite/index") + // The path to the test runner + // Passed to --extensionTestsPath + const extensionTestsPath = resolve(__dirname, "./integration/index") - // Download VS Code, unzip it and run the integration test - await runTests({ extensionDevelopmentPath, extensionTestsPath }) - } catch (err) { - /* istanbul ignore next */ - console.error("Failed to run tests") - /* istanbul ignore next */ - process.exit(1) - } + // Download VS Code, unzip it and run the integration test + await runTests({ extensionDevelopmentPath, extensionTestsPath }) } -main().then().catch() +main().catch((error: unknown) => { + /* istanbul ignore next */ + console.error("Failed to run tests:", error) + /* istanbul ignore next */ + process.exit(1) +}) diff --git a/src/test/suite/adapter/vs-code-adapter.test.ts b/src/test/suite/adapter/vs-code-adapter.test.ts deleted file mode 100644 index 59cb4c3..0000000 --- a/src/test/suite/adapter/vs-code-adapter.test.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { equal, fail } from "assert" -import { resolve } from "path" - -import { spy } from "sinon" -import { Uri, window, workspace } from "vscode" - -import { Severity, VsCodeAdapter } from "../../../adapter/vs-code-adapter" - -suite("Test VsCodeAdapter - getActiveDocument()", () => { - test("when text editor is active should open document", async () => { - const uri = Uri.parse(resolve("./src/test/files/testGetActiveDocument.yaml")) - const doc = await workspace.openTextDocument(uri) - await window.showTextDocument(doc, { preview: false }) - - if (window.activeTextEditor) { - const expected = - "key:\n" + - " key2: value" - - equal(VsCodeAdapter.getActiveDocument(window.activeTextEditor), expected) - } else { - fail("window.activeTextEditor is not set") - } - }) -}) - -suite("Test VsCodeAdapter - showMessage()", () => { - const vscodeadapter = new VsCodeAdapter() - test("when Severity.ERROR should show error message", () => { - const spyError = spy(window, "showErrorMessage") - equal(spyError.called, false) - vscodeadapter.showMessage(Severity.ERROR, "Test message") - equal(spyError.called, true) - }) - - test("when Severity.INFO should show info message", () => { - const spyNotify = spy(vscodeadapter, "notify") - equal(spyNotify.called, false) - vscodeadapter.showMessage(Severity.INFO, "Test message") - equal(spyNotify.called, true) - }) -}) - -suite("Test VsCodeAdapter - notify()", () => { - test("when notifySuccess should show info message", () => { - const vscodeadapter = new VsCodeAdapter() - const spyObject = spy(window, "showInformationMessage") - - vscodeadapter.settings.notifySuccess = false - vscodeadapter.notify("Test message") - equal(spyObject.called, false) - - vscodeadapter.settings.notifySuccess = true - vscodeadapter.notify("Test message") - equal(spyObject.called, true) - }) -}) diff --git a/src/test/suite/controller/processor-controller.test.ts b/src/test/suite/controller/processor-controller.test.ts deleted file mode 100644 index d664e6d..0000000 --- a/src/test/suite/controller/processor-controller.test.ts +++ /dev/null @@ -1,149 +0,0 @@ -import { equal, notEqual } from "assert" - -import { ProcessorController } from "../../../controller/processor-controller" - -suite("Test ProcessorController - preprocess() & postprocess()", () => { - test("text should be the same after pre- and postprocessing", () => { - const text = - "foo: {{ .Value }}\n" + - "# comment\n" + - "bar: baz" - const processor = new ProcessorController(text) - - processor.preprocess() - - notEqual(processor.text, text) - - const text2 = - "foo: {{ .Value }}\n" + - "bar: baz" - processor.text = text2 - processor.postprocess() - - equal(processor.text, text) - }) - - test("test setting useArrayProcessor = true", () => { - const text = "test: [ \"CMD\", \"pg_isready\", \"-q\", \"-d\", \"DB_NAME\", \"-U\", \"DB_USER\" ]" - const processor = new ProcessorController(text) - - processor.preprocess() - - equal(processor.arrayprocessor.store.size, 1) - }) - test("test setting useArrayProcessor = false", () => { - let text = "test: [ \"CMD\", \"pg_isready\", \"-q\", \"-d\", \"DB_NAME\", \"-U\", \"DB_USER\" ]" - const processor = new ProcessorController(text) - processor.settings.useArrayProcessor = false - - processor.preprocess() - - equal(processor.arrayprocessor, undefined) - - text = "test: vscode-yaml-sort.array.0" - processor.text = text - - processor.postprocess() - - equal(processor.text, text) - }) - - test("test setting useCommentProcessor = true", () => { - const text = - "# comment\n" + - "bar: baz" - const processor = new ProcessorController(text) - - processor.preprocess() - - equal(processor.commentprocessor.store.length, 1) - }) - test("test setting useCommentProcessor = false", () => { - const text = - "# comment\n" + - "bar: baz" - const processor = new ProcessorController(text) - processor.settings.useCommentProcessor = false - - processor.preprocess() - - equal(processor.commentprocessor, undefined) - - processor.text = "bar: baz" - processor.postprocess() - - notEqual(processor.text, text) - }) - - test("test setting useHelmProcessor = true", () => { - const text = - "foo: {{ .Value }}\n" + - "bar: baz" - const processor = new ProcessorController(text) - - processor.preprocess() - - equal(processor.helmprocessor.store.size, 1) - }) - test("test setting useHelmProcessor = false", () => { - let text = "foo: {{ .Value }}" - const processor = new ProcessorController(text) - processor.settings.useHelmProcessor = false - - processor.preprocess() - - equal(processor.helmprocessor, undefined) - - text = "foo: vscode-yaml-sort.helm.0" - processor.text = text - - processor.postprocess() - - equal(processor.text, text) - }) - - test("test setting useHelmProcessor = true", () => { - const text = - "foo: {{ .Value }}\n" + - "bar: baz" - const processor = new ProcessorController(text) - - processor.preprocess() - - equal(processor.helmprocessor.store.size, 1) - }) - - test("test setting useOctalProcessor = false", () => { - let text = "foo: 0644" - const processor = new ProcessorController(text) - processor.settings.useOctalProcessor = false - - processor.preprocess() - - equal(processor.octalprocessor, undefined) - - text = "foo: vscode-yaml-sort.octal.0" - processor.text = text - - processor.postprocess() - - equal(processor.text, text) - }) - - test("GitHub issue #164: Comment processor duplicates comments inside block scalars", () => { - const text = - "block:\n" + - " - |\n" + - " # comment 1\n" + - " some command\n" + - " # comment 2\n" + - " some other command" - const processor = new ProcessorController(text) - - processor.preprocess() - processor.postprocess() - - equal(processor.text, text) - - }) -}) diff --git a/src/test/suite/util/error-util.test.ts b/src/test/suite/util/error-util.test.ts deleted file mode 100644 index 3be2d68..0000000 --- a/src/test/suite/util/error-util.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { equal } from "assert" - -import { spy } from "sinon" - -import { ErrorUtil } from "../../../util/error-util" - -suite("Test ErrorUtil - handleError()", () => { - test("when input is of type error, should execute showMessage function", () => { - const errorutil = new ErrorUtil() - const spyObject = spy(errorutil.vscodeadapter, "showMessage") - - errorutil.handleError("no error") - equal(spyObject.called, false) - - errorutil.handleError(new Error()) - equal(spyObject.called, true) - }) -}) \ No newline at end of file diff --git a/src/test/suite/util/file-util.test.ts b/src/test/suite/util/file-util.test.ts deleted file mode 100644 index 2d3e4d9..0000000 --- a/src/test/suite/util/file-util.test.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { deepEqual } from "assert" - -import { Settings } from "../../../settings" -import { FileUtil } from "../../../util/file-util" - -suite("Test FileUtil - getFiles()", () => { - const settings = new Settings() - settings.getExtensions = function () { - return ["yaml", "yml", "customyaml"] - } - const fileutil = new FileUtil(settings) - - test("should return all files", () => { - const expected = [ - "src/test/suite/util/resources/foo.yaml", - "src/test/suite/util/resources/subfolder/foo2.yaml", - "src/test/suite/util/resources/bar.yml", - "src/test/suite/util/resources/subfolder/.customyaml" - ] - deepEqual(fileutil.getFiles("./src/test/suite/util/resources"), expected) - }) - - test("should list all files with extension *.yaml or *.yml in a directory and all its subdirectories", () => { - const expected = [ - "src/test/files/getYamlFilesInDirectory/file2.yaml", - "src/test/files/getYamlFilesInDirectory/file.yaml", - "src/test/files/getYamlFilesInDirectory/folder2/file.yaml", - "src/test/files/getYamlFilesInDirectory/folder1/file2.yaml", - "src/test/files/getYamlFilesInDirectory/folder1/file.yaml", - "src/test/files/getYamlFilesInDirectory/file.yml" - ] - deepEqual(fileutil.getFiles("./src/test/files/getYamlFilesInDirectory"), expected) - }) -}) diff --git a/src/test/suite/controller/document-transformer.test.ts b/src/test/unit/core/document-transformer.test.ts similarity index 72% rename from src/test/suite/controller/document-transformer.test.ts rename to src/test/unit/core/document-transformer.test.ts index 6e23184..8053e8b 100644 --- a/src/test/suite/controller/document-transformer.test.ts +++ b/src/test/unit/core/document-transformer.test.ts @@ -1,20 +1,22 @@ import { equal, throws } from "assert" -import { spy } from "sinon" -import { window } from "vscode" - -import { YamlOperations, formatDocuments, sortDocuments } from "../../../controller/document-transformer" -import { Settings } from "../../../settings" -import { YamlUtil } from "../../../util/yaml-util" +import { YamlOperations, formatDocuments, sortDocuments } from "../../../core/document-transformer" +import { Settings } from "../../../core/settings" +import { YamlSorter } from "../../../core/yaml-sorter" /** * Real yaml operations, except that sorting fails for every document containing the marker. */ const failingSortFor = (marker: string): YamlOperations => { - const yamlutil = new YamlUtil() - const original = yamlutil.sortYaml.bind(yamlutil) - yamlutil.sortYaml = (text: string, customSort?: number) => (text.includes(marker) ? null : original(text, customSort)) - return yamlutil + const yamlSorter = new YamlSorter() + const original = yamlSorter.sortYaml.bind(yamlSorter) + yamlSorter.sortYaml = (text: string, customSort?: number) => { + if (text.includes(marker)) { + throw new Error(`cannot sort ${marker}`) + } + return original(text, customSort) + } + return yamlSorter } suite("Test DocumentTransformer - sortDocuments()", () => { @@ -35,18 +37,23 @@ suite("Test DocumentTransformer - sortDocuments()", () => { test("should throw when a selection ends with a colon", () => { throws(() => sortDocuments("key:", { customSort: 0, isSelection: true }), /YAML selection is invalid/) }) + + test("should sort a whole document whose last key has no value", () => { + // only a selection can be cut off in the middle of a value, a whole document ending with a colon is valid + equal(sortDocuments("b: 1\na:", { customSort: 0, isSelection: false }), "---\na: null\nb: 1") + }) }) suite("Test DocumentTransformer - sortDocuments() when a document cannot be sorted", () => { - test("should return null instead of dropping the failing document", () => { + test("should throw instead of dropping the failing document", () => { const text = "---\nb: 1\na: 2\n---\nfail: 1\nc: 2\n---\ne: 1\nd: 2" - equal(sortDocuments(text, { customSort: 0, isSelection: false }, failingSortFor("fail")), null) + throws(() => sortDocuments(text, { customSort: 0, isSelection: false }, failingSortFor("fail")), /cannot sort fail/) }) - test("should return null instead of an empty text for a selection", () => { + test("should throw instead of returning an empty text for a selection", () => { // an empty result would replace the selection with nothing - equal(sortDocuments(" fail: 1\n a: 2", { customSort: 0, isSelection: true }, failingSortFor("fail")), null) + throws(() => sortDocuments(" fail: 1\n a: 2", { customSort: 0, isSelection: true }, failingSortFor("fail")), /cannot sort fail/) }) }) @@ -91,10 +98,10 @@ suite("Test DocumentTransformer - delimiters and directives", () => { }) test("should drop only the leading delimiter when useLeadingDashes is false", () => { - const yamlutil = new YamlUtil() - yamlutil.settings.useLeadingDashes = false + const yamlSorter = new YamlSorter() + yamlSorter.settings.useLeadingDashes = false - equal(sort("--- # first\nb: 1\n--- # second\nd: 1\nc: 2", yamlutil), "b: 1\n--- # second\nc: 2\nd: 1") + equal(sort("--- # first\nb: 1\n--- # second\nd: 1\nc: 2", yamlSorter), "b: 1\n--- # second\nc: 2\nd: 1") }) test("should keep an empty document", () => { @@ -106,10 +113,10 @@ suite("Test DocumentTransformer - delimiters and directives", () => { }) test("should keep yaml directives when useLeadingDashes is false, because they require a delimiter", () => { - const yamlutil = new YamlUtil() - yamlutil.settings.useLeadingDashes = false + const yamlSorter = new YamlSorter() + yamlSorter.settings.useLeadingDashes = false - equal(sort("%YAML 1.2\n---\nb: 1\n---\nd: 1\nc: 2", yamlutil), "%YAML 1.2\n---\nb: 1\n---\nc: 2\nd: 1") + equal(sort("%YAML 1.2\n---\nb: 1\n---\nd: 1\nc: 2", yamlSorter), "%YAML 1.2\n---\nb: 1\n---\nc: 2\nd: 1") }) test("should keep the delimiters of a selection as they are", () => { @@ -126,15 +133,8 @@ suite("Test DocumentTransformer - formatDocuments()", () => { equal(formatDocuments("---\nkey1: value\n---\nkey2: value"), "---\nkey1: value\n---\nkey2: value") }) - test("should not show a message, so formatting on save stays silent (#65)", () => { - const info = spy(window, "showInformationMessage") - try { - formatDocuments("---\nkey1: value\n---\nkey2: value") - - equal(info.callCount, 0) - } finally { - info.restore() - } + test("should throw on invalid yaml instead of dropping the document", () => { + throws(() => formatDocuments("---\nkey1: value\n---\nkey2: [value")) }) }) @@ -143,6 +143,6 @@ suite("Test DocumentTransformer - injected settings", () => { const settings = new Settings() settings.schema = Settings.getJsYamlSchemaFromString("CLOUDFORMATION_SCHEMA") - equal(sortDocuments("b: !Ref x\na: 1", { customSort: 0, isSelection: false }, new YamlUtil(settings)), "---\na: 1\nb: !Ref x") + equal(sortDocuments("b: !Ref x\na: 1", { customSort: 0, isSelection: false }, new YamlSorter(settings)), "---\na: 1\nb: !Ref x") }) }) diff --git a/src/test/suite/controller/file-sorter.test.ts b/src/test/unit/core/file-sorter.test.ts similarity index 73% rename from src/test/suite/controller/file-sorter.test.ts rename to src/test/unit/core/file-sorter.test.ts index 6c0f24d..bcfd34a 100644 --- a/src/test/suite/controller/file-sorter.test.ts +++ b/src/test/unit/core/file-sorter.test.ts @@ -1,18 +1,18 @@ import { doesNotThrow, equal, ok, throws } from "assert" -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" +import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs" import { tmpdir } from "os" import { join } from "path" -import { sortFile } from "../../../controller/file-sorter" +import { sortFile } from "../../../core/file-sorter" suite("Test FileSorter - sortFile()", () => { test("should throw when file does not contain a valid yaml", () => { - throws(() => sortFile("./src/test/suite/util/resources/subfolder/foo2.yaml")) + throws(() => sortFile("./src/test/files/file-finder/subfolder/foo2.yaml")) }) test("should not throw when file contains a valid yaml", () => { - doesNotThrow(() => sortFile("./src/test/suite/util/resources/subfolder/.customyaml")) + doesNotThrow(() => sortFile("./src/test/files/file-finder/subfolder/.customyaml")) }) test("should retain a trailing newline when the original file has one", () => { @@ -64,4 +64,18 @@ suite("Test FileSorter - sortFile()", () => { rmSync(dir, { recursive: true, force: true }) } }) + + test("should not write a file that is already sorted", () => { + const dir = mkdtempSync(join(tmpdir(), "yaml-sort-")) + const file = join(dir, "sorted.yaml") + try { + writeFileSync(file, "---\na: 1\nb: 2\n") + // a write to a read-only file would throw + chmodSync(file, 0o444) + + doesNotThrow(() => sortFile(file)) + } finally { + rmSync(dir, { recursive: true, force: true }) + } + }) }) diff --git a/src/test/suite/lib.test.ts b/src/test/unit/core/indentation.test.ts similarity index 84% rename from src/test/suite/lib.test.ts rename to src/test/unit/core/indentation.test.ts index 6d7e3d3..3feca0e 100644 --- a/src/test/suite/lib.test.ts +++ b/src/test/unit/core/indentation.test.ts @@ -3,8 +3,8 @@ import { equal, throws} from "assert" import { prependWhitespacesOnEachLine, removeLeadingSpacesOnEachLine -} from "../../lib" -import { removeTrailingCharacters } from "../../util/yaml-text" +} from "../../../core/indentation" +import { removeTrailingCharacters } from "../../../core/yaml-text" suite("Test removeTrailingCharacters", () => { const actual = "text" @@ -47,8 +47,11 @@ suite("Test prependWhitespacesOnEachLine", () => { throws(() => prependWhitespacesOnEachLine(actual, -1)) }) const actual2 = "text\n" - test("should return ` text\\n ` when `text\\n` and 2 are passed", () => { - equal(prependWhitespacesOnEachLine(actual2, 2), " text\n ") + test("should not indent the empty line after a trailing line break", () => { + equal(prependWhitespacesOnEachLine(actual2, 2), " text\n") + }) + test("should not add trailing whitespaces to empty lines", () => { + equal(prependWhitespacesOnEachLine("a: |\n line1\n\n line2", 2), " a: |\n line1\n\n line2") }) }) diff --git a/src/test/suite/adapter/js-yaml-adapter.test.ts b/src/test/unit/core/js-yaml-adapter.test.ts similarity index 76% rename from src/test/suite/adapter/js-yaml-adapter.test.ts rename to src/test/unit/core/js-yaml-adapter.test.ts index f0678ec..5ba9371 100644 --- a/src/test/suite/adapter/js-yaml-adapter.test.ts +++ b/src/test/unit/core/js-yaml-adapter.test.ts @@ -4,11 +4,11 @@ import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema" import { CORE_SCHEMA, YAMLException } from "js-yaml" -import { JsYamlAdapter } from "../../../adapter/js-yaml-adapter" -import { Settings } from "../../../settings" +import { JsYamlAdapter } from "../../../core/js-yaml-adapter" +import { Settings } from "../../../core/settings" suite("Test JsYamlAdapter - validateYaml()", () => { - const jsyamladapter = new JsYamlAdapter(new Settings()) + const jsYamlAdapter = new JsYamlAdapter(new Settings()) test("when text is a valid yaml document should return true", () => { const actual = @@ -20,7 +20,7 @@ suite("Test JsYamlAdapter - validateYaml()", () => { " kitty:\n" + " age: 3" - equal(jsyamladapter.validateYaml(actual), true) + equal(jsYamlAdapter.validateYaml(actual), true) }) test("when text are two seperated valid yaml documents should return true", () => { @@ -34,13 +34,13 @@ suite("Test JsYamlAdapter - validateYaml()", () => { " kitty:\n" + " age: 3" - equal(jsyamladapter.validateYaml(actual), true) + equal(jsYamlAdapter.validateYaml(actual), true) }) test("when text is an invalid yaml document should throw YAMLException", () => { const actual = "network: ethernets:" - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when indentation is not correct should throw YAMLException", () => { const actual = @@ -48,7 +48,7 @@ suite("Test JsYamlAdapter - validateYaml()", () => { "bob\n" + " age:23" - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when text contains duplicate key should throw YAMLException", () => { const actual = @@ -58,39 +58,39 @@ suite("Test JsYamlAdapter - validateYaml()", () => { " bob:\n" + " age: 25" - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when text is a yaml document using CLOUDFORMATION_SCHEMA and schema is CORE_SCHEMA should throw YAMLException", () => { const actual = "RoleName: !Sub \"AdministratorAccess\"" - jsyamladapter.settings.getSchema = function () { + jsYamlAdapter.settings.getSchema = function () { return CORE_SCHEMA } - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when text is a yaml document using CLOUDFORMATION_SCHEMA and schema is CLOUDFORMATION_SCHEMA should return true", () => { const actual = "RoleName: !Sub \"AdministratorAccess\"" - jsyamladapter.settings.getSchema = function () { + jsYamlAdapter.settings.getSchema = function () { return CLOUDFORMATION_SCHEMA } - equal(jsyamladapter.validateYaml(actual), true) + equal(jsYamlAdapter.validateYaml(actual), true) }) test("when text is a yaml document using HOMEASSISTANT_SCHEMA and schema is CORE_SCHEMA should throw YAMLException", () => { const actual = "password: !env_var PASSWORD default_password" - jsyamladapter.settings.getSchema = function () { + jsYamlAdapter.settings.getSchema = function () { return CORE_SCHEMA } - throws(() => jsyamladapter.validateYaml(actual), YAMLException) + throws(() => jsYamlAdapter.validateYaml(actual), YAMLException) }) test("when text is a yaml document using HOMEASSISTANT_SCHEMA and schema is HOMEASSISTANT_SCHEMA should return true", () => { const actual = "password: !env_var PASSWORD default_password" - jsyamladapter.settings.getSchema = function () { + jsYamlAdapter.settings.getSchema = function () { return HOMEASSISTANT_SCHEMA } - equal(jsyamladapter.validateYaml(actual), true) + equal(jsYamlAdapter.validateYaml(actual), true) }) }) diff --git a/src/test/suite/processor/array-processor.test.ts b/src/test/unit/core/processor/array-processor.test.ts similarity index 92% rename from src/test/suite/processor/array-processor.test.ts rename to src/test/unit/core/processor/array-processor.test.ts index 1cb270a..f7d2815 100644 --- a/src/test/suite/processor/array-processor.test.ts +++ b/src/test/unit/core/processor/array-processor.test.ts @@ -1,6 +1,6 @@ import { equal } from "assert" -import { ArrayProcessor } from "../../../processor/array-processor" +import { ArrayProcessor } from "../../../../core/processor/array-processor" suite("Test ArrayProcessor - preprocess()", () => { test("should find all array values", () => { diff --git a/src/test/suite/processor/block-processor.test.ts b/src/test/unit/core/processor/block-processor.test.ts similarity index 97% rename from src/test/suite/processor/block-processor.test.ts rename to src/test/unit/core/processor/block-processor.test.ts index b2bfe4d..21e1742 100644 --- a/src/test/suite/processor/block-processor.test.ts +++ b/src/test/unit/core/processor/block-processor.test.ts @@ -1,6 +1,6 @@ import { equal } from "assert" -import { BlockProcessor } from "../../../processor/block-processor" +import { BlockProcessor } from "../../../../core/processor/block-processor" suite("Test BlockProcessor - preprocess()", () => { test("input string with block sequence > should be the same after pre and postprocessing", () => { diff --git a/src/test/suite/processor/comment-processor.test.ts b/src/test/unit/core/processor/comment-processor.test.ts similarity index 91% rename from src/test/suite/processor/comment-processor.test.ts rename to src/test/unit/core/processor/comment-processor.test.ts index a049459..4fa219e 100644 --- a/src/test/suite/processor/comment-processor.test.ts +++ b/src/test/unit/core/processor/comment-processor.test.ts @@ -1,6 +1,6 @@ import { deepEqual, equal } from "assert" -import { CommentProcessor } from "../../../processor/comment-processor" +import { CommentProcessor } from "../../../../core/processor/comment-processor" suite("Test CommentProcessor - findComments()", () => { test("should add all comments to array 'comments'", () => { @@ -26,24 +26,6 @@ suite("Test CommentProcessor - findComments()", () => { }) }) -suite("Test CommentProcessor - isLineComment()", () => { - test("when input is '# comment' should return true", () => { - equal(CommentProcessor.isLineComment("# comment"), true) - }) - test("when input is ' # comment' should return true", () => { - equal(CommentProcessor.isLineComment(" # comment"), true) - }) - test("when input is 'text # comment' should return false", () => { - equal(CommentProcessor.isLineComment("text # comment"), false) - }) - test("when input is 'text' should return false", () => { - equal(CommentProcessor.isLineComment("text"), false) - }) - test("when input is an empty string should return false", () => { - equal(CommentProcessor.isLineComment(""), false) - }) -}) - suite("Test CommentProcessor - addLineToComments()", () => { test("should add comments to array 'comments'", () => { const text = @@ -250,6 +232,36 @@ suite("Test CommentProcessor - searchFuzzyForKeyword()", () => { equal(commentprocessor.searchFuzzyForKeyword(line), 17) }) + + test("should find the line of the key, not the key as part of another key", () => { + const commentprocessor = new CommentProcessor("a: x\ndata: 2") + + equal(commentprocessor.searchFuzzyForKeyword("a: \"x\""), 0) + }) + + test("should find the key with a different indentation", () => { + const commentprocessor = new CommentProcessor("b:\n a: x\n data: 2") + + equal(commentprocessor.searchFuzzyForKeyword(" a: \"x\""), 3) + }) +}) + +suite("Test CommentProcessor - comments of values that change while sorting", () => { + test("should keep a comment above its key when the quotes of the value are removed", () => { + const processor = new CommentProcessor("# about a\na: \"x\"\ndata: 2") + processor.findComments() + processor.text = "a: x\ndata: 2" + + equal(processor.postprocess(), "# about a\na: x\ndata: 2") + }) + + test("should keep an inline comment at its key when the quotes of the value are removed", () => { + const processor = new CommentProcessor("a: \"x\" # about a\ndata: 2") + processor.findComments() + processor.text = "a: x\ndata: 2" + + equal(processor.postprocess(), "a: x # about a\ndata: 2") + }) }) suite("Test CommentProcessor - isCommentFound()", () => { @@ -493,20 +505,13 @@ suite("Test CommentProcessor - GitHub issue #222", () => { }) }) -suite("Test CommentProcessor - getInlineComment()", () => { - const detectionCases: { line: string, expectedComment: string | undefined }[] = [ - { line: "key: value # comment", expectedComment: "# comment" }, - { line: "key: \"value # quoted\"", expectedComment: undefined }, - { line: "key: 'value # quoted'", expectedComment: undefined }, - { line: "key: value#noComment", expectedComment: undefined }, - { line: "key: \"a\" # comment", expectedComment: "# comment" }, - { line: "# full line comment", expectedComment: undefined } - ] +suite("Test CommentProcessor - inline comments of values with apostrophes", () => { + test("should keep the inline comment of a value containing an apostrophe", () => { + const processor = new CommentProcessor("c: it's fine # note\nb: 1") + processor.findComments() + processor.text = "b: 1\nc: it's fine" - detectionCases.forEach(({ line, expectedComment }) => { - test("should only detect comments outside of quotes", () => { - equal(CommentProcessor.getInlineComment(line), expectedComment) - }) + equal(processor.postprocess(), "b: 1\nc: it's fine # note") }) }) diff --git a/src/test/suite/processor/generic-processor.test.ts b/src/test/unit/core/processor/generic-processor.test.ts similarity index 62% rename from src/test/suite/processor/generic-processor.test.ts rename to src/test/unit/core/processor/generic-processor.test.ts index 19e2d54..a2a9619 100644 --- a/src/test/suite/processor/generic-processor.test.ts +++ b/src/test/unit/core/processor/generic-processor.test.ts @@ -1,28 +1,28 @@ import { equal } from "assert" -import { HelmProcessor } from "../../../processor/helm-processor" +import { HelmProcessor } from "../../../../core/processor/helm-processor" suite("Test GenericProcessor - replaceSubstituteWithValue()", () => { test("GitHub issue #154: Ensure a substitue is surrounded with the same quotation marks", () => { const text = "TABLE_SOURCES: 'vscode-yaml-sort.helm.0 TABLE'" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "{{ LEVEL }}") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "{{ LEVEL }}") const expected = "TABLE_SOURCES: '{{ LEVEL }} TABLE'" - equal(helmprocessor.text, expected) + equal(helmProcessor.text, expected) }) test("GitHub issue #130: dollar signs in a value must not be interpreted as replacement patterns", () => { const text = "label: vscode-yaml-sort.helm.0" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "{{''$''}}") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "{{''$''}}") const expected = "label: {{''$''}}" - equal(helmprocessor.text, expected) + equal(helmProcessor.text, expected) }) }) @@ -30,13 +30,13 @@ suite("Test GenericProcessor - substitutes with more than ten matches", () => { test("postprocess restores every value at its own key when more than ten substitutes exist", () => { // vscode-yaml-sort.helm.1 must not match the prefix of vscode-yaml-sort.helm.10 or .11 const lines = Array.from({ length: 12 }, (_, i) => `key${i}: "{{ .Values.value${i} }}"`) - const helmprocessor = new HelmProcessor(lines.join("\n")) + const helmProcessor = new HelmProcessor(lines.join("\n")) - helmprocessor.preprocess() + helmProcessor.preprocess() // simulate sorting, which moves the higher substitutes in front of the lower ones - helmprocessor.text = helmprocessor.text.split("\n").reverse().join("\n") - helmprocessor.postprocess() + helmProcessor.text = helmProcessor.text.split("\n").reverse().join("\n") + helmProcessor.postprocess() - equal(helmprocessor.text, [...lines].reverse().join("\n")) + equal(helmProcessor.text, [...lines].reverse().join("\n")) }) }) diff --git a/src/test/suite/processor/helm-processor.test.ts b/src/test/unit/core/processor/helm-processor.test.ts similarity index 56% rename from src/test/suite/processor/helm-processor.test.ts rename to src/test/unit/core/processor/helm-processor.test.ts index 1989ab5..c2fd266 100644 --- a/src/test/suite/processor/helm-processor.test.ts +++ b/src/test/unit/core/processor/helm-processor.test.ts @@ -1,6 +1,6 @@ import { equal, match } from "assert" -import { HelmProcessor } from "../../../processor/helm-processor" +import { HelmProcessor } from "../../../../core/processor/helm-processor" suite("Test HelmProcessor - preprocess()", () => { test("should replace all helm values", () => { @@ -12,36 +12,36 @@ suite("Test HelmProcessor - preprocess()", () => { "app: clientsso\n" + "name: {{ .Values.ui.environment.keycloakClientId }}\n" + "namespace: {{ required 'Keycloak namespace required' .Values.keycloak.namespace }}\n" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() + helmProcessor.preprocess() - equal(helmprocessor.store.size, 2) - equal(helmprocessor.store.get("vscode-yaml-sort.helm.0"), "{{ .Values.ui.environment.keycloakClientId }}") - equal(helmprocessor.store.get("vscode-yaml-sort.helm.1"), "{{ required 'Keycloak namespace required' .Values.keycloak.namespace }}") + equal(helmProcessor.store.size, 2) + equal(helmProcessor.store.get("vscode-yaml-sort.helm.0"), "{{ .Values.ui.environment.keycloakClientId }}") + equal(helmProcessor.store.get("vscode-yaml-sort.helm.1"), "{{ required 'Keycloak namespace required' .Values.keycloak.namespace }}") }) }) suite("Test HelmProcessor - createSubstitute()", () => { test("should store each value under a consecutive substitute", () => { - const helmprocessor = new HelmProcessor("") + const helmProcessor = new HelmProcessor("") - equal(helmprocessor.createSubstitute("{{ .a }}"), "vscode-yaml-sort.helm.0") - equal(helmprocessor.createSubstitute("{{ .b }}"), "vscode-yaml-sort.helm.1") + equal(helmProcessor.createSubstitute("{{ .a }}"), "vscode-yaml-sort.helm.0") + equal(helmProcessor.createSubstitute("{{ .b }}"), "vscode-yaml-sort.helm.1") - equal(helmprocessor.store.size, 2) - equal(helmprocessor.store.get("vscode-yaml-sort.helm.0"), "{{ .a }}") - equal(helmprocessor.store.get("vscode-yaml-sort.helm.1"), "{{ .b }}") + equal(helmProcessor.store.size, 2) + equal(helmProcessor.store.get("vscode-yaml-sort.helm.0"), "{{ .a }}") + equal(helmProcessor.store.get("vscode-yaml-sort.helm.1"), "{{ .b }}") }) }) suite("Test HelmProcessor - preprocess() keeps surrounding text", () => { test("should keep trailing newline character", () => { - const helmprocessor = new HelmProcessor("foo: {{ .bar }}\n") + const helmProcessor = new HelmProcessor("foo: {{ .bar }}\n") - helmprocessor.preprocess() + helmProcessor.preprocess() - equal(helmprocessor.text, "foo: vscode-yaml-sort.helm.0\n") + equal(helmProcessor.text, "foo: vscode-yaml-sort.helm.0\n") }) }) @@ -55,15 +55,15 @@ suite("Test HelmProcessor - postprocess()", () => { "app: clientsso\n" + "name: {{ .Values.ui.environment.keycloakClientId }}\n" + "namespace: {{ required 'Keycloak namespace required' .Values.keycloak.namespace }}\n" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() + helmProcessor.preprocess() - match(helmprocessor.text, /vscode-yaml-sort.helm/) + match(helmProcessor.text, /vscode-yaml-sort.helm/) - helmprocessor.postprocess() + helmProcessor.postprocess() - equal(helmprocessor.text, text) + equal(helmProcessor.text, text) }) }) @@ -73,51 +73,51 @@ suite("Test HelmProcessor - replaceSubstituteWithValue()", () => { "key1: 'vscode-yaml-sort.helm.0'\n" + "key2: \"vscode-yaml-sort.helm.1\"\n" + "key3: vscode-yaml-sort.helm.2" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "test0") - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.1", "test1") - helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.2", "test2") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "test0") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.1", "test1") + helmProcessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.2", "test2") const expected = "key1: test0\n" + "key2: test1\n" + "key3: test2" - equal(helmprocessor.text, expected) + equal(helmProcessor.text, expected) }) }) suite("Test HelmProcessor - GitHub issue #130", () => { test("preprocess should keep the quotation marks of a quoted helm value in the store", () => { const text = "section:\n label: '{{''$''}}'" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() + helmProcessor.preprocess() - equal(helmprocessor.store.size, 1) - equal(helmprocessor.store.get("vscode-yaml-sort.helm.0"), "'{{''$''}}'") - equal(helmprocessor.text, "section:\n label: vscode-yaml-sort.helm.0") + equal(helmProcessor.store.size, 1) + equal(helmProcessor.store.get("vscode-yaml-sort.helm.0"), "'{{''$''}}'") + equal(helmProcessor.text, "section:\n label: vscode-yaml-sort.helm.0") }) test("postprocess should restore a quoted helm value containing a dollar sign", () => { const text = "section:\n label: '{{''$''}}'" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() - helmprocessor.postprocess() + helmProcessor.preprocess() + helmProcessor.postprocess() - equal(helmprocessor.text, text) + equal(helmProcessor.text, text) }) }) suite("Test HelmProcessor - GitHub issue #283", () => { test("should keep double quotes around a helm value", () => { const text = "foo: \"{{ .Fighters}}\"" - const helmprocessor = new HelmProcessor(text) + const helmProcessor = new HelmProcessor(text) - helmprocessor.preprocess() - helmprocessor.postprocess() + helmProcessor.preprocess() + helmProcessor.postprocess() - equal(helmprocessor.text, text) + equal(helmProcessor.text, text) }) }) diff --git a/src/test/suite/processor/octal-processor.test.ts b/src/test/unit/core/processor/octal-processor.test.ts similarity index 68% rename from src/test/suite/processor/octal-processor.test.ts rename to src/test/unit/core/processor/octal-processor.test.ts index 2b33b6f..740428d 100644 --- a/src/test/suite/processor/octal-processor.test.ts +++ b/src/test/unit/core/processor/octal-processor.test.ts @@ -1,6 +1,6 @@ import { equal } from "assert" -import { OctalProcessor } from "../../../processor/octal-processor" +import { OctalProcessor } from "../../../../core/processor/octal-processor" suite("Test OctalProcessor - preprocess()", () => { test("should find one octal values", () => { @@ -64,3 +64,32 @@ suite("Test OctalProcessor - matched occurrence", () => { equal(octalprocessor.text, text) }) }) + +suite("Test OctalProcessor - octal values in other positions", () => { + const cases: [string, string][] = [ + ["with an inline comment", "mode: 0755 # rwxr-xr-x"], + ["as list item", "modes:\n - 0755\n - 0644"], + ] + + cases.forEach(([name, text]) => { + test(`should keep the leading zero of an octal value ${name}`, () => { + const octalprocessor = new OctalProcessor(text) + + octalprocessor.preprocess() + + equal(/\b0[0-7]{3}\b/.test(octalprocessor.text), false) + octalprocessor.postprocess() + equal(octalprocessor.text, text) + }) + }) + + test("should not touch a value that only starts with an octal number", () => { + const text = "id: 0755-1" + const octalprocessor = new OctalProcessor(text) + + octalprocessor.preprocess() + + equal(octalprocessor.text, text) + }) +}) + diff --git a/src/test/unit/core/processor/processor-pipeline.test.ts b/src/test/unit/core/processor/processor-pipeline.test.ts new file mode 100644 index 0000000..4bd3414 --- /dev/null +++ b/src/test/unit/core/processor/processor-pipeline.test.ts @@ -0,0 +1,142 @@ +import { equal, notEqual, ok } from "assert" + +import { ProcessorPipeline } from "../../../../core/processor/processor-pipeline" +import { Settings } from "../../../../core/settings" + +/** + * @returns Settings with all processors enabled, except the given one. + */ +const settingsWithout = (setting: "useArrayProcessor" | "useCommentProcessor" | "useHelmProcessor" | "useOctalProcessor" | "useBlockProcessor"): Settings => { + const settings = new Settings() + settings[setting] = false + return settings +} + +suite("Test ProcessorPipeline - preprocess() & postprocess()", () => { + test("text should be the same after pre- and postprocessing", () => { + const text = + "foo: {{ .Value }}\n" + + "# comment\n" + + "bar: baz" + const processor = new ProcessorPipeline(text) + + processor.preprocess() + + notEqual(processor.text, text) + + const text2 = + "foo: {{ .Value }}\n" + + "bar: baz" + processor.text = text2 + processor.postprocess() + + equal(processor.text, text) + }) + + test("should replace tabs with spaces", () => { + const processor = new ProcessorPipeline("a:\n\tb: 1") + + processor.preprocess() + + equal(processor.text, "a:\n b: 1") + }) + + test("should hide and restore arrays when useArrayProcessor = true", () => { + const text = "test: [ \"CMD\", \"pg_isready\" ]" + const processor = new ProcessorPipeline(text) + + processor.preprocess() + + equal(processor.text, "test: vscode-yaml-sort.array.0") + processor.postprocess() + equal(processor.text, text) + }) + + test("should leave arrays alone when useArrayProcessor = false", () => { + const text = "test: [ \"CMD\", \"pg_isready\" ]" + const processor = new ProcessorPipeline(text, settingsWithout("useArrayProcessor")) + + processor.preprocess() + + equal(processor.text, text) + }) + + test("should restore comments when useCommentProcessor = true", () => { + const processor = new ProcessorPipeline("# comment\nbar: baz") + + processor.preprocess() + processor.text = "bar: baz" + processor.postprocess() + + equal(processor.text, "# comment\nbar: baz") + }) + + test("should drop comments when useCommentProcessor = false", () => { + const processor = new ProcessorPipeline("# comment\nbar: baz", settingsWithout("useCommentProcessor")) + + processor.preprocess() + processor.text = "bar: baz" + processor.postprocess() + + equal(processor.text, "bar: baz") + }) + + test("should hide and restore helm values when useHelmProcessor = true", () => { + const processor = new ProcessorPipeline("foo: {{ .Value }}\nbar: baz") + + processor.preprocess() + + equal(processor.text, "foo: vscode-yaml-sort.helm.0\nbar: baz") + }) + + test("should leave helm values alone when useHelmProcessor = false", () => { + const processor = new ProcessorPipeline("foo: {{ .Value }}", settingsWithout("useHelmProcessor")) + + processor.preprocess() + + equal(processor.text, "foo: {{ .Value }}") + }) + + test("should leave octal values alone when useOctalProcessor = false", () => { + const processor = new ProcessorPipeline("foo: 0644", settingsWithout("useOctalProcessor")) + + processor.preprocess() + + equal(processor.text, "foo: 0644") + }) + + test("should hide block scalars when useBlockProcessor = true", () => { + const processor = new ProcessorPipeline("a: |\n text") + + processor.preprocess() + + ok(processor.text.startsWith("a: vscode-yaml-sort.block.0")) + }) + + test("should restore the values in reverse order, so nested substitutes are resolved", () => { + // the helm value is inside the array, which the array processor hides first + const text = "a: [ \"{{ .Value }}\", b ]" + const processor = new ProcessorPipeline(text) + + processor.preprocess() + processor.postprocess() + + equal(processor.text, text) + }) + + test("GitHub issue #164: Comment processor duplicates comments inside block scalars", () => { + const text = + "block:\n" + + " - |\n" + + " # comment 1\n" + + " some command\n" + + " # comment 2\n" + + " some other command" + const processor = new ProcessorPipeline(text) + + processor.preprocess() + processor.postprocess() + + equal(processor.text, text) + }) +}) diff --git a/src/test/suite/processor/spacing-processor.test.ts b/src/test/unit/core/processor/spacing-processor.test.ts similarity index 97% rename from src/test/suite/processor/spacing-processor.test.ts rename to src/test/unit/core/processor/spacing-processor.test.ts index 161f9c4..2819ecf 100644 --- a/src/test/suite/processor/spacing-processor.test.ts +++ b/src/test/unit/core/processor/spacing-processor.test.ts @@ -1,6 +1,6 @@ import { deepEqual, equal } from "assert" -import { SpacingProcessor } from "../../../processor/spacing-processor" +import { SpacingProcessor } from "../../../../core/processor/spacing-processor" suite("Test SpacingProcessor - getMatcher()", () => { test("should return regex with correct number of spaces", () => { diff --git a/src/test/suite/processor/tabs-to-spaces-preprocessor.test.ts b/src/test/unit/core/processor/tabs-to-spaces-processor.test.ts similarity index 54% rename from src/test/suite/processor/tabs-to-spaces-preprocessor.test.ts rename to src/test/unit/core/processor/tabs-to-spaces-processor.test.ts index 56f5797..3d9f673 100644 --- a/src/test/suite/processor/tabs-to-spaces-preprocessor.test.ts +++ b/src/test/unit/core/processor/tabs-to-spaces-processor.test.ts @@ -1,6 +1,6 @@ import { equal } from "assert" -import { TabsToSpacesProcessor } from "../../../processor/tabs-to-spaces-processor" +import { TabsToSpacesProcessor } from "../../../../core/processor/tabs-to-spaces-processor" suite("Test TabsToSpacesPreprocessor - preprocess()", () => { const text = @@ -23,4 +23,15 @@ suite("Test TabsToSpacesPreprocessor - preprocess()", () => { tabstospacespreprocessor.preprocess() equal(tabstospacespreprocessor.text, expected) }) -}) \ No newline at end of file +}) + +suite("Test TabsToSpacesPreprocessor - tabs inside values", () => { + test("should keep tabs that are not part of the indentation, e.g. inside a quoted string", () => { + const processor = new TabsToSpacesProcessor("a:\n\tb: \"x\ty\"") + + processor.preprocess() + + equal(processor.text, "a:\n b: \"x\ty\"") + }) +}) + diff --git a/src/test/unit/core/processor/yaml-comment.test.ts b/src/test/unit/core/processor/yaml-comment.test.ts new file mode 100644 index 0000000..4bc2b4a --- /dev/null +++ b/src/test/unit/core/processor/yaml-comment.test.ts @@ -0,0 +1,47 @@ +import { equal } from "assert" + +import { getInlineComment, isLineComment } from "../../../../core/processor/yaml-comment" + +suite("Test isLineComment()", () => { + test("when input is '# comment' should return true", () => { + equal(isLineComment("# comment"), true) + }) + test("when input is ' # comment' should return true", () => { + equal(isLineComment(" # comment"), true) + }) + test("when input is 'text # comment' should return false", () => { + equal(isLineComment("text # comment"), false) + }) + test("when input is 'text' should return false", () => { + equal(isLineComment("text"), false) + }) + test("when input is an empty string should return false", () => { + equal(isLineComment(""), false) + }) +}) + + +suite("Test getInlineComment()", () => { + const detectionCases: { line: string, expectedComment: string | undefined }[] = [ + { line: "key: value # comment", expectedComment: "# comment" }, + { line: "key: \"value # quoted\"", expectedComment: undefined }, + { line: "key: 'value # quoted'", expectedComment: undefined }, + { line: "key: value#noComment", expectedComment: undefined }, + { line: "key: \"a\" # comment", expectedComment: "# comment" }, + { line: "# full line comment", expectedComment: undefined }, + // an apostrophe inside a plain value does not start a quoted value + { line: "key: it's fine # comment", expectedComment: "# comment" }, + { line: "key: don't # comment 'quoted'", expectedComment: "# comment 'quoted'" }, + // escaped quotes do not end a quoted value + { line: "key: \"a \\\" # b\" # comment", expectedComment: "# comment" }, + { line: "key: 'a '' # b' # comment", expectedComment: "# comment" }, + { line: "- 'a # b' # comment", expectedComment: "# comment" }, + { line: "key: [\"a # b\", 'c # d'] # comment", expectedComment: "# comment" } + ] + + detectionCases.forEach(({ line, expectedComment }) => { + test(`should only detect comments outside of quotes: ${line}`, () => { + equal(getInlineComment(line), expectedComment) + }) + }) +}) diff --git a/src/test/suite/settings.test.ts b/src/test/unit/core/settings.test.ts similarity index 81% rename from src/test/suite/settings.test.ts rename to src/test/unit/core/settings.test.ts index 3c2f393..2bf00f2 100644 --- a/src/test/suite/settings.test.ts +++ b/src/test/unit/core/settings.test.ts @@ -3,9 +3,8 @@ import { deepEqual, equal } from "assert" import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" import { HOMEASSISTANT_SCHEMA } from "homeassistant-js-yaml-schema" import { CORE_SCHEMA, DEFAULT_SCHEMA, FAILSAFE_SCHEMA, JSON_SCHEMA, Schema} from "js-yaml" -import { Uri, workspace } from "vscode" -import { Settings } from "../../settings" +import { DEFAULT_CONFIGURATION, Settings } from "../../../core/settings" suite("Test Settings - getter", () => { const settings: Settings = new Settings() @@ -84,6 +83,9 @@ suite("Test Settings - getCustomSortKeywords()", () => { test("when index is not in [1, 2, 3] return `[]`", () => { deepEqual(settings.getCustomSortKeywords(0), []) + deepEqual(settings.getCustomSortKeywords(4), []) + deepEqual(settings.getCustomSortKeywords(-1), []) + deepEqual(settings.getCustomSortKeywords(1.5), []) }) }) @@ -129,17 +131,19 @@ suite("Test Settings - doSortOnSave()", () => { }) }) -suite("Test Settings - forDocument()", () => { - test("should read the configuration for the given document", () => { - const document = { uri: Uri.file("/project/a.yaml"), languageId: "yaml" } - const calls: unknown[][] = [] - const getConfiguration = (section?: string, scope?: unknown) => { - calls.push([section, scope]) - return workspace.getConfiguration(section) - } +suite("Test Settings - configuration", () => { + test("should use the configured values", () => { + const configuration = { indent: 4, customSortKeywords_2: ["b", "a"] } as Record + const settings = new Settings({ get: (key: string) => configuration[key] as T | undefined }) - Settings.forDocument(document, getConfiguration) + equal(settings.indent, 4) + deepEqual(settings.getCustomSortKeywords(2), ["b", "a"]) + equal(settings.lineWidth, DEFAULT_CONFIGURATION.lineWidth) + }) + + test("should not share lists between snapshots and the defaults", () => { + new Settings().getCustomSortKeywords(1).push("changed") - deepEqual(calls, [["vscode-yaml-sort", document]]) + deepEqual(new Settings().getCustomSortKeywords(1), ["apiVersion", "kind", "metadata", "spec", "data"]) }) }) diff --git a/src/test/suite/util/sort-util.test.ts b/src/test/unit/core/sort-order.test.ts similarity index 91% rename from src/test/suite/util/sort-util.test.ts rename to src/test/unit/core/sort-order.test.ts index bc36801..8a1d290 100644 --- a/src/test/suite/util/sort-util.test.ts +++ b/src/test/unit/core/sort-order.test.ts @@ -1,20 +1,20 @@ import { deepEqual, equal } from "assert" -import { Settings } from "../../../settings" -import { SortUtil, sortArraysRecursively } from "../../../util/sort-util" +import { Settings } from "../../../core/settings" +import { SortOrder, sortArraysRecursively } from "../../../core/sort-order" suite("Test Sort - localeSort()", () => { test("when `locale` is `sv` and a is `ä` and b is `z` should return 1", () => { const settings = new Settings() settings.locale = "sv" - const sort = new SortUtil(settings) + const sort = new SortOrder(settings) equal(sort.localeSort("ä", "z"), 1) }) test("when `locale` is `en` and a is `ä` and b is `z` should return -1", () => { const settings = new Settings() settings.locale = "en" - const sort = new SortUtil(settings) + const sort = new SortOrder(settings) equal(sort.localeSort("ä", "z"), -1) }) }) @@ -24,7 +24,7 @@ suite("Test Sort - customSort()", () => { settings.getCustomSortKeywords = function () { return ["kind", "data"] } - const sort = new SortUtil(settings) + const sort = new SortOrder(settings) sort.custom = 1 test("when `custom` is `1` and keywords are `['kind', 'data']` and a is `data` and b is `kind` should return 1", () => { diff --git a/src/test/unit/core/yaml-file-finder.test.ts b/src/test/unit/core/yaml-file-finder.test.ts new file mode 100644 index 0000000..b886907 --- /dev/null +++ b/src/test/unit/core/yaml-file-finder.test.ts @@ -0,0 +1,62 @@ +import { deepEqual } from "assert" +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs" +import { tmpdir } from "os" +import { join } from "path" + +import { Settings } from "../../../core/settings" +import { YamlFileFinder } from "../../../core/yaml-file-finder" + +suite("Test YamlFileFinder - getFiles()", () => { + const settings = new Settings() + settings.getExtensions = function () { + return ["yaml", "yml", "customyaml"] + } + const fileFinder = new YamlFileFinder(settings) + + test("should return all files", () => { + const expected = [ + "src/test/files/file-finder/foo.yaml", + "src/test/files/file-finder/subfolder/foo2.yaml", + "src/test/files/file-finder/bar.yml", + "src/test/files/file-finder/subfolder/.customyaml" + ] + deepEqual(fileFinder.getFiles("./src/test/files/file-finder"), expected) + }) + + test("should list all files with extension *.yaml or *.yml in a directory and all its subdirectories", () => { + const expected = [ + "src/test/files/getYamlFilesInDirectory/file2.yaml", + "src/test/files/getYamlFilesInDirectory/file.yaml", + "src/test/files/getYamlFilesInDirectory/folder2/file.yaml", + "src/test/files/getYamlFilesInDirectory/folder1/file2.yaml", + "src/test/files/getYamlFilesInDirectory/folder1/file.yaml", + "src/test/files/getYamlFilesInDirectory/file.yml" + ] + deepEqual(fileFinder.getFiles("./src/test/files/getYamlFilesInDirectory"), expected) + }) +}) + +suite("Test YamlFileFinder - getFiles() in a project", () => { + let dir: string + + setup(() => { + dir = mkdtempSync(join(tmpdir(), "yaml-finder-")) + mkdirSync(join(dir, "node_modules", "dependency"), { recursive: true }) + writeFileSync(join(dir, "node_modules", "dependency", "config.yaml"), "a: 1") + writeFileSync(join(dir, "app.yaml"), "a: 1") + }) + + teardown(() => rmSync(dir, { recursive: true, force: true })) + + test("should not return files of installed dependencies in node_modules", () => { + deepEqual(new YamlFileFinder().getFiles(dir), [join(dir, "app.yaml")]) + }) + + test("should return every file once, even if an extension is configured twice", () => { + const settings = new Settings() + settings.extensions = ["yaml", "yaml"] + + deepEqual(new YamlFileFinder(settings).getFiles(dir), [join(dir, "app.yaml")]) + }) +}) + diff --git a/src/test/suite/util/yaml-util.test.ts b/src/test/unit/core/yaml-sorter.test.ts similarity index 62% rename from src/test/suite/util/yaml-util.test.ts rename to src/test/unit/core/yaml-sorter.test.ts index 6e3530f..d9fdd35 100644 --- a/src/test/suite/util/yaml-util.test.ts +++ b/src/test/unit/core/yaml-sorter.test.ts @@ -1,33 +1,33 @@ -import { equal } from "assert" +import { equal, throws } from "assert" import { CLOUDFORMATION_SCHEMA } from "cloudformation-js-yaml-schema" import { CORE_SCHEMA } from "js-yaml" -import { Settings } from "../../../settings" -import { YamlUtil } from "../../../util/yaml-util" +import { Settings } from "../../../core/settings" +import { YamlSorter } from "../../../core/yaml-sorter" suite("Test sortArrays", () => { - const yamlutil = new YamlUtil() + const yamlSorter = new YamlSorter() const text = "array:\n" + " - python\n" + " - perl" test("when sortArrays is set to false, should not sort arrays", () => { - const actual = yamlutil.sortYaml(text) + const actual = yamlSorter.sortYaml(text) equal(actual, text) }) test("when sortArrays is set to true, should sort arrays", () => { - yamlutil.settings.sortArrays = true - const actual = yamlutil.sortYaml("foo") + yamlSorter.settings.sortArrays = true + const actual = yamlSorter.sortYaml("foo") const expected = "foo" equal(actual, expected) }) test("when sortArrays is set to true, should sort arrays", () => { - yamlutil.settings.sortArrays = true - const actual = yamlutil.sortYaml(text) + yamlSorter.settings.sortArrays = true + const actual = yamlSorter.sortYaml(text) const expected = "array:\n" + " - perl\n" + @@ -36,13 +36,13 @@ suite("Test sortArrays", () => { }) test("when sortArrays is set to true, should sort nested arrays", () => { - yamlutil.settings.sortArrays = true + yamlSorter.settings.sortArrays = true const text = "foo:\n" + " array:\n" + " - python\n" + " - perl" - const actual = yamlutil.sortYaml(text) + const actual = yamlSorter.sortYaml(text) const expected = "foo:\n" + " array:\n" + @@ -52,19 +52,6 @@ suite("Test sortArrays", () => { }) }) -suite("Test YamlUtil - setLeadingDashes()", () => { - test("when setting useLeadingDashes is `true` should prepend `---\n` to text", () => { - const yamlutil = new YamlUtil() - const text = "lorem ipsum" - yamlutil.settings.useLeadingDashes = false - equal(yamlutil.setLeadingDashes(text), text) - - yamlutil.settings.useLeadingDashes = true - const expected = "---\nlorem ipsum" - equal(yamlutil.setLeadingDashes(text), expected) - }) -}) - suite("Test dumpYaml", () => { test("when useCustomSortRecursively is set to`true` should recursively use customSort", () => { const actual = @@ -91,8 +78,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("when locale is `en` should sort character `ä` over `z`", () => { @@ -107,8 +94,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.locale = "en" settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("when locale is `sv` should sort character `z` over `ä`", () => { @@ -122,8 +109,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.locale = "sv" - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should ignore case when sorting", () => { @@ -137,8 +124,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) }) @@ -169,8 +156,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should sort with by locale behaviour", () => { const actual = @@ -183,15 +170,15 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) expected = "z: value\n" + "ä: value" - yamlutil.settings.locale = "sv" - equal(yamlutil.sortYaml(actual, 1), expected) + yamlSorter.settings.locale = "sv" + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should ignore case when sorting", () => { const actual = @@ -204,8 +191,8 @@ suite("Test dumpYaml", () => { const settings = new Settings() settings.useCustomSortRecursively = true - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 1), expected) }) }) @@ -218,10 +205,10 @@ suite("Test sortYaml", () => { let expected = "spec: spec\n" + - "data: data\n" + "data: data" - const yamlutil = new YamlUtil() - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter() + equal(yamlSorter.sortYaml(actual, 1), expected) actual = "data: data\n" + @@ -231,9 +218,9 @@ suite("Test sortYaml", () => { expected = "spec:\n" + " - aa: b\n" + - "data: data\n" + "data: data" - equal(yamlutil.sortYaml(actual, 1), expected) + equal(yamlSorter.sortYaml(actual, 1), expected) actual = "data:\n" + @@ -251,9 +238,9 @@ suite("Test sortYaml", () => { "data:\n" + " job: Developer\n" + " skills:\n" + - " - pascal\n" + " - pascal" - equal(yamlutil.sortYaml(actual, 1), expected) + equal(yamlSorter.sortYaml(actual, 1), expected) actual = "data: data\n" + @@ -265,9 +252,9 @@ suite("Test sortYaml", () => { "spec:\n" + " - a\n" + " - b\n" + - "data: data\n" + "data: data" - equal(yamlutil.sortYaml(actual, 1), expected) + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should wrap words after 500 characters (`vscode-yaml-sort.lineWidth`)", () => { @@ -289,14 +276,14 @@ suite("Test sortYaml", () => { "aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo\n" + " dolores et e" - const yamlutil = new YamlUtil() - equal(yamlutil.sortYaml(actual, 1), expected) + const yamlSorter = new YamlSorter() + equal(yamlSorter.sortYaml(actual, 1), expected) }) test("should not format a date in CORE_SCHEMA", () => { const actual = "AWSTemplateFormatVersion: 2010-09-09" let expected = "AWSTemplateFormatVersion: 2010-09-09T00:00:00.000Z" - let yamlutil = new YamlUtil() - equal(yamlutil.sortYaml(actual, 0), expected) + let yamlSorter = new YamlSorter() + equal(yamlSorter.sortYaml(actual, 0), expected) expected = actual @@ -304,9 +291,9 @@ suite("Test sortYaml", () => { settings.getSchema = function () { return CORE_SCHEMA } - yamlutil = new YamlUtil(settings) + yamlSorter = new YamlSorter(settings) - equal(yamlutil.sortYaml(actual, 0), expected) + equal(yamlSorter.sortYaml(actual, 0), expected) }) test("should sort a yaml with CLOUDFORMATION_SCHEMA", () => { const actual = @@ -329,8 +316,8 @@ suite("Test sortYaml", () => { settings.getQuotingType = function () { return "\"" } - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 0), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 0), expected) }) test("compatibility with older yaml versions should be configurable", () => { const actual = @@ -348,15 +335,15 @@ suite("Test sortYaml", () => { settings.getQuotingType = function () { return "\"" } - const yamlutil = new YamlUtil(settings) - equal(yamlutil.sortYaml(actual, 0), expected) + const yamlSorter = new YamlSorter(settings) + equal(yamlSorter.sortYaml(actual, 0), expected) expected = "key:\n" + " off: egg\n" + " on: foo" - yamlutil.settings.noCompatMode = true - equal(yamlutil.sortYaml(actual, 0), expected) + yamlSorter.settings.noCompatMode = true + equal(yamlSorter.sortYaml(actual, 0), expected) }) }) @@ -382,8 +369,8 @@ suite("Test formatYaml", () => { const settings = new Settings() settings.useLeadingDashes = false - let yamlutil = new YamlUtil(settings) - equal(yamlutil.formatYaml(actual, false), expected) + let yamlSorter = new YamlSorter(settings) + equal(yamlSorter.formatYaml(actual, false), expected) expected = "---\n" + @@ -395,102 +382,69 @@ suite("Test formatYaml", () => { " kitty:\n" + " age: 3" - yamlutil = new YamlUtil() - equal(yamlutil.formatYaml(actual, true), expected) + yamlSorter = new YamlSorter() + equal(yamlSorter.formatYaml(actual, true), expected) }) - test("should return `null` on invalid yaml", () => { - const yamlutil = new YamlUtil() - equal(yamlutil.formatYaml("key: 1\nkey: 1", true), null) + test("should throw on invalid yaml", () => { + const yamlSorter = new YamlSorter() + throws(() => yamlSorter.formatYaml("key: 1\nkey: 1", true), /duplicated mapping key/) }) }) -suite("Test YamlUtil - getNumberOfLeadingSpaces()", () => { +suite("Test YamlSorter - getNumberOfLeadingSpaces()", () => { test("should return the number of leading spaces of 'text'", () => { - equal(YamlUtil.getNumberOfLeadingSpaces(" foo"), 2) - equal(YamlUtil.getNumberOfLeadingSpaces("foo"), 0) - equal(YamlUtil.getNumberOfLeadingSpaces("foo "), 0) + equal(YamlSorter.getNumberOfLeadingSpaces(" foo"), 2) + equal(YamlSorter.getNumberOfLeadingSpaces("foo"), 0) + equal(YamlSorter.getNumberOfLeadingSpaces("foo "), 0) }) }) -suite("Test YamlUtil - isValueMultiline", () => { - test("when text starts with '-' should return true", () => { - const text = - "- foo: bar\n" + - " bar: baz" - equal(YamlUtil.isValueMultiline(text), true) - }) -}) - -suite("Test YamlUtil - transformMultilineValue()", () => { - const yamlutil = new YamlUtil() - yamlutil.settings.indent = 2 - test("Should prepend spaces on each line", () => { - const text = - "- foo: bar\n" + - " bar: baz" - const expected = - " - foo: bar\n" + - " bar: baz" - equal(yamlutil.transformMultilineValue(text), expected) - }) - test("When text ends with newline should remove trailing indent", () => { - const text = - "- foo: bar\n" + - " bar: baz\n" - const expected = - " - foo: bar\n" + - " bar: baz\n" - equal(yamlutil.transformMultilineValue(text), expected) - }) -}) -/* -*/ -suite("Test YamlUtil - sortYaml() with quoted helm values", () => { +suite("Test YamlSorter - sortYaml() with quoted helm values", () => { test("GitHub issue #130: should not mangle a quoted helm value containing a dollar sign", () => { - const yamlutil = new YamlUtil() + const yamlSorter = new YamlSorter() const text = "section:\n label: '{{''$''}}'" - equal(yamlutil.sortYaml(text), text) + equal(yamlSorter.sortYaml(text), text) }) }) -suite("Test YamlUtil - sortYaml() with double-quoted helm values", () => { +suite("Test YamlSorter - sortYaml() with double-quoted helm values", () => { test("GitHub issue #283: should keep double quotes around a helm value", () => { - const yamlutil = new YamlUtil() + const yamlSorter = new YamlSorter() const text = "foo: \"{{ .Fighters}}\"" - equal(yamlutil.sortYaml(text), text) + equal(yamlSorter.sortYaml(text), text) }) }) -suite("Test YamlUtil - sortYaml() with kustomize patches", () => { +suite("Test YamlSorter - sortYaml() with kustomize patches", () => { test("GitHub issue #188: should keep a block scalar containing colons intact while sorting", () => { - const util = new YamlUtil() + const util = new YamlSorter() const kustomize = "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\nnamespace: monitor\nresources:\n - bundle.yaml\n\npatches:\n - target:\n kind: ClusterRoleBinding\n name: prometheus-operator\n patch: |-\n - op: replace\n path: /subjects/0/namespace\n value: monitor" const sorted = util.sortYaml(kustomize) - equal(sorted !== null && sorted.includes("patch: |-\n - op: replace\n path: /subjects/0/namespace\n value: monitor"), true) + equal(sorted.includes("patch: |-\n - op: replace\n path: /subjects/0/namespace\n value: monitor"), true) }) }) -suite("Test YamlUtil - sortYaml() with inline comments", () => { +suite("Test YamlSorter - sortYaml() with inline comments", () => { test("GitHub issue #222: should keep an inline comment while sorting", () => { - const instance = new YamlUtil() + const instance = new YamlSorter() const game = "game:\n core:\n serviceRpcTimeoutMillis: 6900000 # Extra-long for debugging locally" equal(instance.sortYaml(game), game) }) }) -suite("Test YamlUtil - sortYaml() with docker-compose files", () => { +suite("Test YamlSorter - sortYaml() with docker-compose files", () => { test("GitHub issue #221: should keep a comment above a service while sorting with forceQuotes", () => { const composeSettings = new Settings() composeSettings.forceQuotes = true composeSettings.getQuotingType = function () { return "\"" } - const composeUtil = new YamlUtil(composeSettings) + const composeUtil = new YamlSorter(composeSettings) const composeYaml = "services:\n" + " # https://hub.docker.com/_/mysql\n" + @@ -504,68 +458,97 @@ suite("Test YamlUtil - sortYaml() with docker-compose files", () => { }) }) -suite("Test YamlUtil - sortYaml() keeps substituted values intact", () => { - const yamlutil = new YamlUtil() +suite("Test YamlSorter - sortYaml() keeps substituted values intact", () => { + const yamlSorter = new YamlSorter() test("should keep more than ten helm values at their keys", () => { const lines = Array.from({ length: 12 }, (_, i) => `key${String(i).padStart(2, "0")}: "{{ .Values.value${i} }}"`) - equal(yamlutil.sortYaml([...lines].reverse().join("\n")), lines.join("\n")) + equal(yamlSorter.sortYaml([...lines].reverse().join("\n")), lines.join("\n")) }) test("should keep an octal value when the same digits appear quoted earlier", () => { const text = "a: '0755'\nz: 0755" - equal(yamlutil.sortYaml(text), text) + equal(yamlSorter.sortYaml(text), text) }) }) -suite("Test YamlUtil - sortYaml() with custom sort and scalar values", () => { - const yamlutil = new YamlUtil() +suite("Test YamlSorter - sortYaml() with custom sort and scalar values", () => { + const yamlSorter = new YamlSorter() test("should keep number and boolean values of custom sort keywords", () => { - equal(yamlutil.sortYaml("spec: x\nkind: true\napiVersion: 3", 1), "apiVersion: 3\nkind: true\nspec: x\n") + equal(yamlSorter.sortYaml("spec: x\nkind: true\napiVersion: 3", 1), "apiVersion: 3\nkind: true\nspec: x") }) test("should keep an empty mapping as value of a custom sort keyword", () => { - equal(yamlutil.sortYaml("spec: x\nmetadata: {}", 1), "metadata: {}\nspec: x\n") + equal(yamlSorter.sortYaml("spec: x\nmetadata: {}", 1), "metadata: {}\nspec: x") }) test("should order custom sort keywords even if their values are falsy", () => { const text = "spec: x\nmetadata: null\nkind: false\napiVersion: 0" - equal(yamlutil.sortYaml(text, 1), "apiVersion: 0\nkind: false\nmetadata: null\nspec: x\n") + equal(yamlSorter.sortYaml(text, 1), "apiVersion: 0\nkind: false\nmetadata: null\nspec: x") }) test("should not add a value to a document containing only a comment", () => { - equal(yamlutil.sortYaml("# only a comment"), "\n# only a comment") + equal(yamlSorter.sortYaml("# only a comment"), "\n# only a comment") }) test("should keep a document containing only a scalar", () => { - equal(yamlutil.sortYaml("42"), "42") + equal(yamlSorter.sortYaml("42"), "42") + }) + + test("should keep a scalar value containing a colon on the line of its keyword", () => { + equal(yamlSorter.sortYaml("spec: x\nkind: http://example.com", 1), "kind: http://example.com\nspec: x") + }) + + test("should keep a block scalar value of a keyword", () => { + equal(yamlSorter.sortYaml("b: 1\nkind: |\n line1\n line2", 1), "kind: |\n line1\n line2\nb: 1") + }) + + test("should quote a keyword that needs quotes", () => { + const settings = new Settings() + settings.customSortKeywords1 = ["a: b"] + + equal(new YamlSorter(settings).sortYaml("c: 2\n'a: b': 1", 1), "'a: b': 1\nc: 2") + }) + + test("should end without a line break when every key is a keyword, like the default sort", () => { + equal(yamlSorter.sortYaml("kind: x\napiVersion: v1", 1), "apiVersion: v1\nkind: x") + }) +}) + +suite("Test YamlSorter - sortYaml() with an empty mapping", () => { + test("should keep a document containing only an empty mapping", () => { + equal(new YamlSorter().sortYaml("{}"), "{}") + }) + + test("should keep a document containing only an empty mapping when formatting", () => { + equal(new YamlSorter().formatYaml("{}", false), "{}") }) }) -suite("Test YamlUtil - sortYaml() with sortOrderReverse", () => { +suite("Test YamlSorter - sortYaml() with sortOrderReverse", () => { const settings = new Settings() settings.sortOrderReverse = true - const yamlutil = new YamlUtil(settings) + const yamlSorter = new YamlSorter(settings) test("should sort keys in reverse order on every level", () => { - equal(yamlutil.sortYaml("a: 1\nc:\n x: 1\n z: 2\nb: 2"), "c:\n z: 2\n x: 1\nb: 2\na: 1") + equal(yamlSorter.sortYaml("a: 1\nc:\n x: 1\n z: 2\nb: 2"), "c:\n z: 2\n x: 1\nb: 2\na: 1") }) test("should sort the remaining keys of a custom sort in reverse order", () => { - equal(yamlutil.sortYaml("a: 1\nkind: x\nb: 2", 1), "kind: x\nb: 2\na: 1") + equal(yamlSorter.sortYaml("a: 1\nkind: x\nb: 2", 1), "kind: x\nb: 2\na: 1") }) }) -suite("Test YamlUtil - injected settings reach every processing step", () => { +suite("Test YamlSorter - injected settings reach every processing step", () => { test("should use emptyLinesUntilLevel of the injected settings", () => { const settings = new Settings() settings.emptyLinesUntilLevel = 1 - equal(new YamlUtil(settings).sortYaml("b: 1\na: 2"), "a: 2\n\nb: 1") + equal(new YamlSorter(settings).sortYaml("b: 1\na: 2"), "a: 2\n\nb: 1") }) test("should use the processor switches of the injected settings", () => { @@ -573,33 +556,48 @@ suite("Test YamlUtil - injected settings reach every processing step", () => { settings.useCommentProcessor = false // without the comment processor js-yaml drops the comment - equal(new YamlUtil(settings).sortYaml("b: 1\n# comment\na: 2"), "a: 2\nb: 1") + equal(new YamlSorter(settings).sortYaml("b: 1\n# comment\na: 2"), "a: 2\nb: 1") }) test("should format with the indentation of the injected settings", () => { const settings = new Settings() settings.indent = 4 - equal(new YamlUtil(settings).formatYaml("a:\n b: 1", false), "a:\n b: 1") + equal(new YamlSorter(settings).formatYaml("a:\n b: 1", false), "a:\n b: 1") }) }) -suite("Test YamlUtil - sortYaml() with custom sort keeps the document intact", () => { - const yamlutil = new YamlUtil() +suite("Test YamlSorter - sortYaml() with custom sort keeps the document intact", () => { + const yamlSorter = new YamlSorter() test("should put the keywords in their order before the remaining keys", () => { - equal(yamlutil.sortYaml("data: 1\nkind: x\nb:\n c: 1", 1), "kind: x\ndata: 1\nb:\n c: 1") + equal(yamlSorter.sortYaml("data: 1\nkind: x\nb:\n c: 1", 1), "kind: x\ndata: 1\nb:\n c: 1") }) test("should keep a document that only contains keywords", () => { - equal(yamlutil.sortYaml("kind: x", 1), "kind: x\n") + equal(yamlSorter.sortYaml("kind: x", 1), "kind: x") }) test("should leave a list document unchanged", () => { - equal(yamlutil.sortYaml("- kind: x\n- a: 1", 1), "- kind: x\n- a: 1") + equal(yamlSorter.sortYaml("- kind: x\n- a: 1", 1), "- kind: x\n- a: 1") }) test("should leave a scalar document unchanged", () => { - equal(yamlutil.sortYaml("42", 1), "42") + equal(yamlSorter.sortYaml("42", 1), "42") }) }) + +suite("Test YamlSorter - sortYaml() with emptyLinesUntilLevel", () => { + const settings = new Settings() + settings.emptyLinesUntilLevel = 2 + const yamlSorter = new YamlSorter(settings) + + test("should not add empty lines inside a block scalar, which would change its value", () => { + equal(yamlSorter.sortYaml("b: 1\na: |\n foo: bar\n baz: 1"), "a: |\n foo: bar\n baz: 1\n\nb: 1") + }) + + test("should add the empty line above the comment of a key, not between them", () => { + equal(yamlSorter.sortYaml("b: 1\n# note\na: 1"), "# note\na: 1\n\nb: 1") + }) +}) + diff --git a/src/test/suite/util/yaml-text.test.ts b/src/test/unit/core/yaml-text.test.ts similarity index 96% rename from src/test/suite/util/yaml-text.test.ts rename to src/test/unit/core/yaml-text.test.ts index 1ac5339..b31dc87 100644 --- a/src/test/suite/util/yaml-text.test.ts +++ b/src/test/unit/core/yaml-text.test.ts @@ -1,6 +1,6 @@ import { deepEqual } from "assert" -import { splitDocuments } from "../../../util/yaml-text" +import { splitDocuments } from "../../../core/yaml-text" suite("Test splitDocuments()", () => { test("should return a single document without delimiter when the text contains no `---`", () => { diff --git a/src/test/suite/package-manifest.test.ts b/src/test/unit/package-manifest.test.ts similarity index 69% rename from src/test/suite/package-manifest.test.ts rename to src/test/unit/package-manifest.test.ts index 9ce8c51..dec15e6 100644 --- a/src/test/suite/package-manifest.test.ts +++ b/src/test/unit/package-manifest.test.ts @@ -2,15 +2,18 @@ import { deepEqual, ok } from "assert" import { readFileSync } from "fs" import { join } from "path" +import { DEFAULT_CONFIGURATION, Settings } from "../../core/settings" + interface Manifest { activationEvents: string[] contributes: { commands: { command: string }[] + configuration: { properties: Record } menus: { commandPalette: { command: string, when: string }[] } } } -// out/test/suite -> project root +// out/test/unit -> project root const manifest = JSON.parse(readFileSync(join(__dirname, "..", "..", "..", "package.json"), "utf-8")) as Manifest suite("Test package.json - activation events", () => { @@ -42,3 +45,18 @@ suite("Test package.json - command palette", () => { } }) }) + +suite("Test package.json - configuration", () => { + const properties = manifest.contributes.configuration.properties + const prefix = `${Settings.SECTION}.` + + test("should declare every setting with the extension's section", () => { + ok(Object.keys(properties).every((key) => key.startsWith(prefix))) + }) + + test("should declare the same defaults as the core settings", () => { + const declared = Object.fromEntries(Object.entries(properties).map(([key, property]) => [key.slice(prefix.length), property.default])) + + deepEqual(declared, DEFAULT_CONFIGURATION) + }) +}) diff --git a/src/util/error-util.ts b/src/util/error-util.ts deleted file mode 100644 index e1767b3..0000000 --- a/src/util/error-util.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter" - -export class ErrorUtil { - vscodeadapter = new VsCodeAdapter() - - handleError(error: unknown) { - if (error instanceof Error) { - this.vscodeadapter.showMessage(Severity.ERROR, error.message) - } - } -} \ No newline at end of file diff --git a/src/util/file-util.ts b/src/util/file-util.ts deleted file mode 100644 index c3f3f1d..0000000 --- a/src/util/file-util.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { join } from "path" - -import { sync } from "glob" - -import { Settings } from "../settings" - -export class FileUtil { - settings: Settings - - constructor(settings = new Settings()) { - this.settings = settings - } - - /** - * Finds all files with one of the configured extensions in a directory and its subdirectories. - * @param path Directory to search. - * @returns Paths of the files, starting with the given directory. - */ - getFiles(path: string): string[] { - // the directory is passed as cwd instead of being part of the pattern: glob treats a backslash - // in a pattern as an escape character, so a windows path like C:\project would match nothing - return this.settings.getExtensions().flatMap((extension) => - sync(`**/*.${extension}`, { cwd: path, dot: true }).map((file) => join(path, file)) - ) - } -} diff --git a/src/util/yaml-util.ts b/src/util/yaml-util.ts deleted file mode 100644 index ed50e86..0000000 --- a/src/util/yaml-util.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { ErrorUtil } from "./error-util" -import { JsYamlAdapter } from "../adapter/js-yaml-adapter" -import { ProcessorController } from "../controller/processor-controller" -import { prependWhitespacesOnEachLine } from "../lib" -import { Settings } from "../settings" -import { sortArraysRecursively } from "./sort-util" -import { removeTrailingCharacters } from "./yaml-text" - -/** - * @param value Any loaded yaml value. - * @returns True for a mapping, i.e. an object that is no list. - */ -const isMapping = (value: unknown): value is Record => - typeof value === "object" && value !== null && !Array.isArray(value) - -export class YamlUtil { - settings: Settings - jsyamladapter: JsYamlAdapter - errorutil = new ErrorUtil() - - constructor(settings = new Settings()) { - this.settings = settings - this.jsyamladapter = new JsYamlAdapter(settings) - } - - static getNumberOfLeadingSpaces(text: string): number { - if (!text.startsWith(" ")) { - return 0 - } else { - return text.search(/\S/) - } - } - - static isValueMultiline(text: string) { - return (!text.startsWith("|") && text.includes(":")) || text.startsWith("-") - } - - setLeadingDashes(text: string) { - if (this.settings.useLeadingDashes) { - return `---\n${text}` - } - return text - } - - transformMultilineValue(text: string) { - let result = prependWhitespacesOnEachLine(text, this.settings.indent) - if (text.endsWith("\n")) { - result = removeTrailingCharacters(result, this.settings.indent) - } - return result - } - - sortYaml(unsortedYaml: string, customSort = 0): string | null { - try { - const processor = new ProcessorController(unsortedYaml, this.settings) - processor.preprocess() - const unsortedYamlWithoutTabs = processor.text - - const doc = this.jsyamladapter.load(unsortedYamlWithoutTabs) - - if (this.settings.sortArrays) { - sortArraysRecursively(doc) - } - - let sortedYaml = "" - let remainder = doc - if (customSort > 0 && !this.settings.useCustomSortRecursively && isMapping(doc)) { - // check for the key itself, values like 0, false or null must be ordered too - const keywords = this.settings.getCustomSortKeywords(customSort) - .filter((key) => Object.prototype.hasOwnProperty.call(doc, key)) - sortedYaml = keywords.map((key) => this.dumpKeyword(key, doc[key], customSort)).join("") - // the remaining keys are sorted as usual; build them anew instead of deleting from the loaded document - remainder = Object.fromEntries(Object.entries(doc).filter(([key]) => !keywords.includes(key))) - } - - // either sort whole yaml or sort the rest of the yaml (which can be empty) and add it to the sortedYaml - sortedYaml += this.jsyamladapter.dumpYaml(remainder, true, customSort) - - processor.text = sortedYaml - processor.postprocess() - - return processor.text - } catch (error) { - this.errorutil.handleError(error) - return null - } - } - - /** - * Dumps one top-level keyword of a custom sort together with its value. - * @param key The keyword. - * @param value Its value. - * @param customSort 1 to 3, the custom sort that is applied. - * @returns The yaml lines for the keyword, ending with a line break. - */ - dumpKeyword(key: string, value: unknown, customSort: number): string { - const dumpedValue = this.jsyamladapter.dumpValue(value, true, customSort) - if (YamlUtil.isValueMultiline(dumpedValue)) { - return `${key}:\n${this.transformMultilineValue(dumpedValue)}\n` - } - return `${key}: ${dumpedValue}\n` - } - - /** - * Formats a yaml document (without sorting). - * @param {string} yaml Yaml to be formatted. - * @returns {string} Formatted yaml. - */ - formatYaml(yaml: string, useLeadingDashes: boolean): string | null { - try { - const processor = new ProcessorController(yaml, this.settings) - processor.preprocess() - let doc = this.jsyamladapter.dumpYaml(this.jsyamladapter.load(processor.text), false, 0) - processor.text = doc - processor.postprocess() - doc = processor.text - if (useLeadingDashes) { - doc = `---\n${doc}` - } - return doc - } catch (error) { - this.errorutil.handleError(error) - return null - } - } - -} diff --git a/src/vscode/configuration.ts b/src/vscode/configuration.ts new file mode 100644 index 0000000..6df8c7d --- /dev/null +++ b/src/vscode/configuration.ts @@ -0,0 +1,17 @@ +import { ConfigurationScope, WorkspaceConfiguration, workspace } from "vscode" + +import { Settings } from "../core/settings" + +export type ConfigurationProvider = (section: string, scope?: ConfigurationScope) => WorkspaceConfiguration + +/** + * Reads the settings that apply to a document: workspace folder settings in a multi-root + * workspace and language specific settings like `"[yaml]": { "vscode-yaml-sort.indent": 4 }`. + * @param scope The document, or its uri and language id. Undefined reads the global and workspace settings. + * @param getConfiguration Injectable for tests. + * @returns A snapshot of the settings. + */ +export const readSettings = ( + scope?: ConfigurationScope, + getConfiguration: ConfigurationProvider = workspace.getConfiguration +): Settings => new Settings(getConfiguration(Settings.SECTION, scope)) diff --git a/src/vscode/directory-sorter.ts b/src/vscode/directory-sorter.ts new file mode 100644 index 0000000..d0b0c3a --- /dev/null +++ b/src/vscode/directory-sorter.ts @@ -0,0 +1,29 @@ +import { Uri } from "vscode" + +import { readSettings } from "./configuration" +import { ErrorReporter } from "./error-reporter" +import { Severity, VsCodeAdapter } from "./vs-code-adapter" +import { sortFile } from "../core/file-sorter" +import { YamlFileFinder } from "../core/yaml-file-finder" +import { YamlSorter } from "../core/yaml-sorter" + +/** + * Command handler: sorts every yaml file in a directory and its subdirectories. + * A file that cannot be sorted is reported and left unchanged; the other files are still sorted. + * @param uri The directory selected in the explorer. Undefined when the command is run from the command palette. + */ +export const sortYamlFilesInDirectory = (uri: Uri | undefined): void => { + const errorReporter = new ErrorReporter() + if (!uri) { + new VsCodeAdapter().showMessage(Severity.ERROR, "Please run this command from the context menu of a folder in the explorer.") + return + } + const settingsFor = (resource: Uri) => readSettings({ uri: resource, languageId: "yaml" }) + for (const file of new YamlFileFinder(settingsFor(uri)).getFiles(uri.fsPath)) { + try { + sortFile(file, new YamlSorter(settingsFor(Uri.file(file)))) + } catch (error) { + errorReporter.handleError(error) + } + } +} diff --git a/src/vscode/editor-commands.ts b/src/vscode/editor-commands.ts new file mode 100644 index 0000000..0234936 --- /dev/null +++ b/src/vscode/editor-commands.ts @@ -0,0 +1,103 @@ +import { Range, TextEdit, TextEditor, WorkspaceEdit, window, workspace } from "vscode" + +import { readSettings } from "./configuration" +import { ErrorReporter } from "./error-reporter" +import { Severity, VsCodeAdapter } from "./vs-code-adapter" +import { formatDocuments, sortDocuments } from "../core/document-transformer" +import { JsYamlAdapter } from "../core/js-yaml-adapter" +import { Settings } from "../core/settings" +import { YamlSorter } from "../core/yaml-sorter" + +/** + * The commands that work on the document of one editor. + */ +export class EditorCommands { + readonly editor: TextEditor + readonly settings: Settings + readonly yamlSorter: YamlSorter + readonly jsYamlAdapter: JsYamlAdapter + readonly vsCodeAdapter: VsCodeAdapter + readonly errorReporter = new ErrorReporter() + + /** + * @param editor The editor to work on. Defaults to the active editor. + * @throws Error if no editor is given and none is active. + */ + constructor(editor: TextEditor | undefined = window.activeTextEditor) { + if (!editor) { + throw new Error("No texteditor is active") + } + this.editor = editor + // one snapshot of the settings that apply to the edited document + this.settings = readSettings(editor.document) + this.yamlSorter = new YamlSorter(this.settings) + this.jsYamlAdapter = new JsYamlAdapter(this.settings) + this.vsCodeAdapter = new VsCodeAdapter(this.settings) + } + + /** + * Validates the whole document and reports the result. + * @returns True if the document is valid yaml. + */ + validateYamlWrapper(): boolean { + try { + this.jsYamlAdapter.validateYaml(this.editor.document.getText()) + } catch (error) { + this.errorReporter.handleError(error) + return false + } + this.vsCodeAdapter.showMessage(Severity.INFO, "YAML is valid.") + return true + } + + /** + * Formats the whole document, regardless of a selection. + * @returns The applied edits, or none if the document could not be formatted. + */ + async formatYamlWrapper(): Promise { + const document = this.editor.document + let newText: string + try { + newText = formatDocuments(document.getText(), this.yamlSorter) + } catch (error) { + this.errorReporter.handleError(error) + return [] + } + // only the command reports success, the formatter stays silent (see #65) + return this.replace(VsCodeAdapter.getFullDocumentRange(document), newText, "Yaml formatted successfully") + } + + /** + * Sorts the selected lines, or the whole document if nothing is selected. + * @param customSort 0 for the default sort, 1 to 3 for the custom sort keywords. + * @returns The applied edits, or none if the text could not be sorted. + */ + async sortYamlWrapper(customSort = 0): Promise { + const range = VsCodeAdapter.getRange(this.editor) + let newText: string + try { + newText = sortDocuments(this.editor.document.getText(range), { customSort, isSelection: !this.editor.selection.isEmpty }, this.yamlSorter) + } catch (error) { + this.errorReporter.handleError(error) + return [] + } + return this.replace(range, newText, "Keys resorted successfully") + } + + /** + * Replaces a range of the editor's document and reports success once the edit is applied. + * @returns The applied edits, or none if VS Code rejected them. + */ + private async replace(range: Range, text: string, successMessage: string): Promise { + const edit = TextEdit.replace(range, text) + const workspaceEdit = new WorkspaceEdit() + // the edit belongs to this editor's document, even if another editor became active meanwhile + workspaceEdit.set(this.editor.document.uri, [edit]) + if (!await workspace.applyEdit(workspaceEdit)) { + this.vsCodeAdapter.showMessage(Severity.ERROR, "The changes could not be applied to the document.") + return [] + } + this.vsCodeAdapter.showMessage(Severity.INFO, successMessage) + return [edit] + } +} diff --git a/src/vscode/error-reporter.ts b/src/vscode/error-reporter.ts new file mode 100644 index 0000000..de639ee --- /dev/null +++ b/src/vscode/error-reporter.ts @@ -0,0 +1,17 @@ +import { Severity, VsCodeAdapter } from "./vs-code-adapter" + +/** + * Shows errors from the core to the user. + */ +export class ErrorReporter { + vsCodeAdapter = new VsCodeAdapter() + + /** + * @param error Anything that was thrown. Values that are no Error are shown as text, + * so no failure goes unnoticed. + */ + handleError(error: unknown) { + const message = error instanceof Error ? error.message : String(error) + this.vsCodeAdapter.showMessage(Severity.ERROR, message) + } +} diff --git a/src/controller/formatting-provider.ts b/src/vscode/formatting-provider.ts similarity index 51% rename from src/controller/formatting-provider.ts rename to src/vscode/formatting-provider.ts index 757f470..f8a4f4e 100644 --- a/src/controller/formatting-provider.ts +++ b/src/vscode/formatting-provider.ts @@ -1,9 +1,10 @@ -import { DocumentFormattingEditProvider, Range, TextDocument, TextEdit } from "vscode" +import { DocumentFormattingEditProvider, TextDocument, TextEdit } from "vscode" -import { formatDocuments, sortDocuments } from "./document-transformer" -import { Settings } from "../settings" -import { ErrorUtil } from "../util/error-util" -import { YamlUtil } from "../util/yaml-util" +import { readSettings } from "./configuration" +import { ErrorReporter } from "./error-reporter" +import { VsCodeAdapter } from "./vs-code-adapter" +import { formatDocuments, sortDocuments } from "../core/document-transformer" +import { YamlSorter } from "../core/yaml-sorter" /** * Formats (or, depending on `sortOnSave`, sorts) a whole yaml document. @@ -12,29 +13,25 @@ import { YamlUtil } from "../util/yaml-util" * so this provider must never apply them on its own (see #180). */ export class YamlFormattingProvider implements DocumentFormattingEditProvider { - errorutil = new ErrorUtil() + errorReporter = new ErrorReporter() /** * @param document The document VS Code asks to format. It may differ from the active editor. * @returns One edit replacing the whole document, or no edit if the document cannot be processed. */ provideDocumentFormattingEdits(document: TextDocument): TextEdit[] { - const settings = Settings.forDocument(document) - const yamlutil = new YamlUtil(settings) + const settings = readSettings(document) + const yamlSorter = new YamlSorter(settings) const text = document.getText() - let newText: string | null + let newText: string try { newText = settings.doSortOnSave() - ? sortDocuments(text, { customSort: settings.sortOnSave, isSelection: false }, yamlutil) - : formatDocuments(text, yamlutil) + ? sortDocuments(text, { customSort: settings.sortOnSave, isSelection: false }, yamlSorter) + : formatDocuments(text, yamlSorter) } catch (error) { - this.errorutil.handleError(error) + this.errorReporter.handleError(error) return [] } - if (newText === null) { - return [] - } - const fullRange = document.validateRange(new Range(0, 0, document.lineCount, 0)) - return [TextEdit.replace(fullRange, newText)] + return [TextEdit.replace(VsCodeAdapter.getFullDocumentRange(document), newText)] } } diff --git a/src/vscode/vs-code-adapter.ts b/src/vscode/vs-code-adapter.ts new file mode 100644 index 0000000..c9a51fb --- /dev/null +++ b/src/vscode/vs-code-adapter.ts @@ -0,0 +1,67 @@ +import { Disposable, DocumentFormattingEditProvider, Range, Selection, TextDocument, TextEditor, languages, window } from "vscode" + +import { Settings } from "../core/settings" + +export enum Severity { + INFO, ERROR +} + +export class VsCodeAdapter { + settings: Settings + + constructor(settings = new Settings()) { + this.settings = settings + } + + showMessage(severity: Severity, message: string) { + if (severity === Severity.ERROR) { + window.showErrorMessage(message) + } else { + this.notify(message) + } + } + + notify(message: string) { + if (this.settings.notifySuccess) { + window.showInformationMessage(message) + } + } + + static registerFormatter(formatter: DocumentFormattingEditProvider): Disposable { + return languages.registerDocumentFormattingEditProvider("yaml", formatter) + } + + /** + * @param document A text document. + * @returns The range from the beginning to the end of the document. + */ + static getFullDocumentRange(document: TextDocument): Range { + return document.validateRange(new Range(0, 0, document.lineCount, 0)) + } + + /** + * @param textEditor An editor with a non-empty selection. + * @returns The selection, extended to whole lines. A selection that ends on the first character + * of a line does not include that line. + */ + static getSelectedRange(textEditor: TextEditor): Selection { + let endLine = textEditor.selection.end.line + if (textEditor.selection.end.character === 0) { + endLine-- + } + return new Selection( + textEditor.selection.start.line, 0, + endLine, textEditor.document.lineAt(endLine).range.end.character) + } + + /** + * @param textEditor An editor. + * @returns The selected lines, or the whole document if nothing is selected. + */ + static getRange(textEditor: TextEditor): Range { + if (textEditor.selection.isEmpty) { + return VsCodeAdapter.getFullDocumentRange(textEditor.document) + } + return VsCodeAdapter.getSelectedRange(textEditor) + } +}