From f2d988138257ff184884e4adc83c39e3bc247e9b Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Fri, 19 Nov 2021 21:07:22 +0100 Subject: [PATCH] Fix unintendedly inlined Windows scripts - Fix reverting "Disable SQM OS key". - Fix applying "Disable Visual Studio Code data collection" scripts. - Fix reverting "Do not show recently used files in Quick Access". - Add unit tests for automatically checking similar issues in future. --- package-lock.json | 138 +++++++++++++++++- package.json | 1 + src/application/collections/windows.yaml | 12 +- .../collections/NoUnintentedInlining.spec.ts | 69 +++++++++ .../application/collections/raw-loader.d.ts | 4 + 5 files changed, 212 insertions(+), 12 deletions(-) create mode 100644 tests/unit/application/collections/NoUnintentedInlining.spec.ts create mode 100644 tests/unit/application/collections/raw-loader.d.ts diff --git a/package-lock.json b/package-lock.json index 832a9a43..8b1f37f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,6 +46,7 @@ "electron-updater": "^4.3.9", "js-yaml-loader": "^1.2.2", "markdownlint-cli": "^0.29.0", + "raw-loader": "^4.0.2", "remark-cli": "^10.0.0", "remark-lint-no-dead-urls": "^1.1.0", "remark-preset-lint-consistent": "^5.1.0", @@ -2267,9 +2268,9 @@ "dev": true }, "node_modules/@types/json-schema": { - "version": "7.0.6", - "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.6.tgz", - "integrity": "sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==", + "version": "7.0.9", + "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", + "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", "dev": true }, "node_modules/@types/mdast": { @@ -18685,6 +18686,82 @@ "node": ">= 0.8" } }, + "node_modules/raw-loader": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/raw-loader/-/raw-loader-4.0.2.tgz", + "integrity": "sha512-ZnScIV3ag9A4wPX/ZayxL/jZH+euYb6FcUinPcgiQW0+UBtEv0O6Q3lGd3cqJ+GHH+rksEv3Pj99oxJ3u3VIKA==", + "dev": true, + "dependencies": { + "loader-utils": "^2.0.0", + "schema-utils": "^3.0.0" + }, + "engines": { + "node": ">= 10.13.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/webpack" + }, + "peerDependencies": { + "webpack": "^4.0.0 || ^5.0.0" + } + }, + "node_modules/raw-loader/node_modules/emojis-list": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/emojis-list/-/emojis-list-3.0.0.tgz", + "integrity": "sha512-/kyM18EfinwXZbno9FyUGeFh87KC8HRQBQGildHZbEuRyWFOmv1U10o9BBp8XVZDVNNuQKyIGIu5ZYAAXJ0V2Q==", + "dev": true, + "engines": { + "node": ">= 4" + } + }, + "node_modules/raw-loader/node_modules/json5": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", + "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", + "dev": true, + "dependencies": { + "minimist": "^1.2.5" + }, + "bin": { + "json5": "lib/cli.js" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/raw-loader/node_modules/loader-utils": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.2.tgz", + "integrity": "sha512-TM57VeHptv569d/GKh6TAYdzKblwDNiumOdkFnejjD0XwTH87K90w3O7AiJRqdQoXygvi1VQTJTLGhJl7WqA7A==", + "dev": true, + "dependencies": { + "big.js": "^5.2.2", + "emojis-list": "^3.0.0", + "json5": "^2.1.2" + }, + "engines": { + "node": ">=8.9.0" + } + }, + "node_modules/raw-loader/node_modules/schema-utils": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.1.1.tgz", + "integrity": "sha512-Y5PQxS4ITlC+EahLuXaY86TXfR7Dc5lw294alXOq86JAHCihAIZfqv8nNCWvaEJvaC51uN9hbLGeV0cFBdH+Fw==", + "dev": true, + "dependencies": { + "@types/json-schema": "^7.0.8", + "ajv": "^6.12.5", + "ajv-keywords": "^3.5.2" + }, + "engines": { + "node": ">= 10.13.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/webpack" + } + }, "node_modules/rc": { "version": "1.2.8", "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", @@ -27659,9 +27736,9 @@ "dev": true }, "@types/json-schema": { - "version": "7.0.6", - "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.6.tgz", - "integrity": "sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==", + "version": "7.0.9", + "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", + "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", "dev": true }, "@types/mdast": { @@ -40399,6 +40476,55 @@ "unpipe": "1.0.0" } }, + "raw-loader": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/raw-loader/-/raw-loader-4.0.2.tgz", + "integrity": "sha512-ZnScIV3ag9A4wPX/ZayxL/jZH+euYb6FcUinPcgiQW0+UBtEv0O6Q3lGd3cqJ+GHH+rksEv3Pj99oxJ3u3VIKA==", + "dev": true, + "requires": { + "loader-utils": "^2.0.0", + "schema-utils": "^3.0.0" + }, + "dependencies": { + "emojis-list": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/emojis-list/-/emojis-list-3.0.0.tgz", + "integrity": "sha512-/kyM18EfinwXZbno9FyUGeFh87KC8HRQBQGildHZbEuRyWFOmv1U10o9BBp8XVZDVNNuQKyIGIu5ZYAAXJ0V2Q==", + "dev": true + }, + "json5": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz", + "integrity": "sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==", + "dev": true, + "requires": { + "minimist": "^1.2.5" + } + }, + "loader-utils": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.2.tgz", + "integrity": "sha512-TM57VeHptv569d/GKh6TAYdzKblwDNiumOdkFnejjD0XwTH87K90w3O7AiJRqdQoXygvi1VQTJTLGhJl7WqA7A==", + "dev": true, + "requires": { + "big.js": "^5.2.2", + "emojis-list": "^3.0.0", + "json5": "^2.1.2" + } + }, + "schema-utils": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.1.1.tgz", + "integrity": "sha512-Y5PQxS4ITlC+EahLuXaY86TXfR7Dc5lw294alXOq86JAHCihAIZfqv8nNCWvaEJvaC51uN9hbLGeV0cFBdH+Fw==", + "dev": true, + "requires": { + "@types/json-schema": "^7.0.8", + "ajv": "^6.12.5", + "ajv-keywords": "^3.5.2" + } + } + } + }, "rc": { "version": "1.2.8", "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", diff --git a/package.json b/package.json index a20c3bda..48d1bd72 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "electron-updater": "^4.3.9", "js-yaml-loader": "^1.2.2", "markdownlint-cli": "^0.29.0", + "raw-loader": "^4.0.2", "remark-cli": "^10.0.0", "remark-lint-no-dead-urls": "^1.1.0", "remark-preset-lint-consistent": "^5.1.0", diff --git a/src/application/collections/windows.yaml b/src/application/collections/windows.yaml index a094a31c..d5387a4f 100644 --- a/src/application/collections/windows.yaml +++ b/src/application/collections/windows.yaml @@ -1623,7 +1623,7 @@ actions: reg add "HKLM\SOFTWARE\Wow6432Node\Microsoft\VSCommon\15.0\SQM" /v "OptIn" /t REG_DWORD /d 0 /f reg add "HKLM\SOFTWARE\Wow6432Node\Microsoft\VSCommon\16.0\SQM" /v "OptIn" /t REG_DWORD /d 0 /f ) - revertCode: + revertCode: |- if %PROCESSOR_ARCHITECTURE%==x86 ( REM is 32 bit? reg add "HKLM\SOFTWARE\Microsoft\VSCommon\14.0\SQM" /v "OptIn" /t REG_DWORD /d 1 /f reg add "HKLM\SOFTWARE\Microsoft\VSCommon\15.0\SQM" /v "OptIn" /t REG_DWORD /d 1 /f @@ -4327,7 +4327,7 @@ actions: if not %PROCESSOR_ARCHITECTURE%==x86 ( REM is 64 bit? reg delete "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\HomeFolderDesktop\NameSpace\DelegateFolders\{3134ef9c-6b18-4996-ad04-ed5912e00eb5}" /f ) - revertCode: + revertCode: |- reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer" /v "ShowRecent" /d "1" /t "REG_DWORD" /f reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\HomeFolderDesktop\NameSpace\DelegateFolders\{3134ef9c-6b18-4996-ad04-ed5912e00eb5}" /f if not %PROCESSOR_ARCHITECTURE%==x86 ( REM is 64 bit? @@ -6161,11 +6161,11 @@ functions: call: function: RunPowerShell parameters: - code: - $jsonfile = \"$env:APPDATA\Code\User\settings.json\"; + code: |- + $jsonfile = \"$env:APPDATA\Code\User\settings.json\" if (!(Test-Path $jsonfile -PathType Leaf)) { - Write-Host \"No updates. Settings file was not at $jsonfile\"; - exit 0; + Write-Host \"No updates. Settings file was not at $jsonfile\" + exit 0 } $json = Get-Content $jsonfile | Out-String | ConvertFrom-Json $json | Add-Member -Type NoteProperty -Name '{{ $setting }}' -Value {{ $powerShellValue }} -Force diff --git a/tests/unit/application/collections/NoUnintentedInlining.spec.ts b/tests/unit/application/collections/NoUnintentedInlining.spec.ts new file mode 100644 index 00000000..fae04a24 --- /dev/null +++ b/tests/unit/application/collections/NoUnintentedInlining.spec.ts @@ -0,0 +1,69 @@ +import 'mocha'; +import { expect } from 'chai'; +import WindowsData from 'raw-loader!@/application/collections/windows.yaml'; +import MacOsData from 'raw-loader!@/application/collections/macos.yaml'; + +/* + A common mistake when working with yaml files to forget mentioning that a value should + be interpreted as multi-line string using "|". + E.g. + ``` + code: |- + echo Hello + echo World + ``` + If "|" is missing then the code is inlined like `echo Hello echo World``, which can be + unintended. This test checks for similar issues in collection yaml files. + These tests can be considered as "linter" more than "unit-test" and therefore can lead + to false-positives. +*/ +describe('collection files to have no unintended inlining', async () => { + // arrange + const testCases = [ { + name: 'macos', + fileContent: MacOsData, + }, { + name: 'windows', + fileContent: WindowsData, + }, + ]; + for (const testCase of testCases) { + it(`${testCase.name}`, async () => { + const lines = await findBadLineNumbers(testCase.fileContent); + expect(lines).to.be.have.lengthOf(0, + `Did you intend to have multi-lined string in lines: ` + + lines.map(((line) => line.toString())).join(', '), + ); + }); + } +}); + +async function findBadLineNumbers(fileContent: string): Promise { + return [ + ...findLineNumbersEndingWith(fileContent, 'revertCode:'), + ...findLineNumbersEndingWith(fileContent, 'code:'), + ]; +} + +function findLineNumbersEndingWith(content: string, ending: string): number[] { + sanityCheck(content, ending); + const lines = content.split(/\r\n|\r|\n/); + const results = new Array(); + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + if (line.trim().endsWith(ending)) { + results.push((i + 1 /* first line is 1 not 0 */)); + } + } + return results; +} + + +function sanityCheck(content: string, ending: string): void { + if (!content.includes(ending)) { + throw new Error( + `File does not contain string "${ending}" string at all.` + + `Did the word "${ending}" change? Or is this sanity check wrong?`, + ); + } +} diff --git a/tests/unit/application/collections/raw-loader.d.ts b/tests/unit/application/collections/raw-loader.d.ts new file mode 100644 index 00000000..655a9012 --- /dev/null +++ b/tests/unit/application/collections/raw-loader.d.ts @@ -0,0 +1,4 @@ +declare module 'raw-loader!@/*' { + const contents: string; + export default contents; +}