From 0236c98f6e66eed5532f29e23bac92ca9c4d494b Mon Sep 17 00:00:00 2001 From: realmarv Date: Mon, 20 Mar 2023 14:29:45 +0330 Subject: [PATCH 01/13] FileConventions: add failing test Add tests for NonVerboseFlagsInGitHubCI function. --- .../DummyFiles/DummyCIWithNonVerboseFlag.yml | 11 ++++++++ .../DummyCIWithoutNonVerboseFlags.yml | 11 ++++++++ .../FileConventions.Test.fs | 26 +++++++++++++++++++ src/FileConventions/Library.fs | 5 ++++ 4 files changed, 53 insertions(+) create mode 100644 src/FileConventions.Test/DummyFiles/DummyCIWithNonVerboseFlag.yml create mode 100644 src/FileConventions.Test/DummyFiles/DummyCIWithoutNonVerboseFlags.yml diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithNonVerboseFlag.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithNonVerboseFlag.yml new file mode 100644 index 00000000..d06a1c9b --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithNonVerboseFlag.yml @@ -0,0 +1,11 @@ +name: CI + +on: [push, pull_request] + +jobs: + file-conventions: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Print apt version + run: apt -v diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithoutNonVerboseFlags.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithoutNonVerboseFlags.yml new file mode 100644 index 00000000..d8efcfc1 --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithoutNonVerboseFlags.yml @@ -0,0 +1,11 @@ +name: CI + +on: [push, pull_request] + +jobs: + file-conventions: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Print apt version + run: apt --version diff --git a/src/FileConventions.Test/FileConventions.Test.fs b/src/FileConventions.Test/FileConventions.Test.fs index 5e756b3a..4b299a0c 100644 --- a/src/FileConventions.Test/FileConventions.Test.fs +++ b/src/FileConventions.Test/FileConventions.Test.fs @@ -547,3 +547,29 @@ let DetectInconsistentVersionsInFSharpScripts2() = (Some(Seq.singleton "DummyScripts")), Is.EqualTo false ) + + +[] +let NonVerboseFlagsInGitHubCI1() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithNonVerboseFlag.yml" + ) + )) + + Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo true) + + +[] +let NonVerboseFlagsInGitHubCI2() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithoutNonVerboseFlags.yml" + ) + )) + + Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 9fc1f1e9..b2ef0a3b 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -329,3 +329,8 @@ let DetectInconsistentVersionsInFSharpScripts false else DetectInconsistentVersionsInNugetRefsInFSharpScripts fsxFiles + + +let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = + assert (fileInfo.FullName.EndsWith(".yml")) + false From 76c20835db7581c020bca8506bd1a684e3068563 Mon Sep 17 00:00:00 2001 From: realmarv Date: Mon, 20 Mar 2023 14:36:31 +0330 Subject: [PATCH 02/13] FileConventions: implement the function Implement NonVerboseFlagsInGitHubCI function. --- src/FileConventions/Library.fs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index b2ef0a3b..67407ca6 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -333,4 +333,8 @@ let DetectInconsistentVersionsInFSharpScripts let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = assert (fileInfo.FullName.EndsWith(".yml")) - false + let fileText = File.ReadAllText(fileInfo.FullName) + + let nonVerboseFlagsRegex = Regex("\\s-[a-zA-Z]\\s", RegexOptions.Compiled) + + nonVerboseFlagsRegex.IsMatch fileText From 840a2faace766bab050c22a2f68c5699e164dd40 Mon Sep 17 00:00:00 2001 From: realmarv Date: Mon, 20 Mar 2023 15:13:48 +0330 Subject: [PATCH 03/13] FileConventions.Test: add failing test --- .../DummyCIWithAcceptedNonVerboseFlag.yml | 11 +++++++++++ src/FileConventions.Test/FileConventions.Test.fs | 13 +++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag.yml diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag.yml new file mode 100644 index 00000000..38ca7e8e --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag.yml @@ -0,0 +1,11 @@ +name: CI + +on: [push, pull_request] + +jobs: + file-conventions: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Print "Hello World!" + run: env -S "echo Hello World!" diff --git a/src/FileConventions.Test/FileConventions.Test.fs b/src/FileConventions.Test/FileConventions.Test.fs index 4b299a0c..e39ede58 100644 --- a/src/FileConventions.Test/FileConventions.Test.fs +++ b/src/FileConventions.Test/FileConventions.Test.fs @@ -573,3 +573,16 @@ let NonVerboseFlagsInGitHubCI2() = )) Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) + + +[] +let NonVerboseFlagsInGitHubCI3() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithAcceptedNonVerboseFlag.yml" + ) + )) + + Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) From 4d67423f248affd505f6cf571b3b1093092b4839 Mon Sep 17 00:00:00 2001 From: realmarv Date: Mon, 20 Mar 2023 16:17:59 +0330 Subject: [PATCH 04/13] FileConvention: fix the function Fix NonVerboseFlagsInGitHubCI function. --- src/FileConventions/Library.fs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 67407ca6..1ef21d67 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -330,11 +330,26 @@ let DetectInconsistentVersionsInFSharpScripts else DetectInconsistentVersionsInNugetRefsInFSharpScripts fsxFiles +let allowedNonVerboseFlags = seq { "env -S" } let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = assert (fileInfo.FullName.EndsWith(".yml")) - let fileText = File.ReadAllText(fileInfo.FullName) + let fileLines = File.ReadLines(fileInfo.FullName) - let nonVerboseFlagsRegex = Regex("\\s-[a-zA-Z]\\s", RegexOptions.Compiled) + let nonVerboseFlagsRegex = Regex("\\s-[a-zA-Z]\\b", RegexOptions.Compiled) - nonVerboseFlagsRegex.IsMatch fileText + let numInvalidFlags = + fileLines + |> Seq.filter(fun line -> + let nonVerboseFlag = nonVerboseFlagsRegex.IsMatch line + + let allowedNonVerboseFlag = + allowedNonVerboseFlags + |> Seq.map(fun allowedFlag -> line.Contains allowedFlag) + |> Seq.contains true + + nonVerboseFlag && not(allowedNonVerboseFlag) + ) + |> Seq.length + + numInvalidFlags > 0 From a6ee08649e5dccce309ced3960a2a939883c8eb8 Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 23 Mar 2023 13:26:44 +0330 Subject: [PATCH 05/13] FileConventions.Test: add tests --- .../DummyScriptWithNonVerboseFlag.fsx | 17 ++++++++++++ .../DummyScriptWithoutNonVerboseFlag.fsx | 17 ++++++++++++ .../FileConventions.Test.fs | 26 +++++++++++++++++++ src/FileConventions/Library.fs | 5 +++- 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/FileConventions.Test/DummyFiles/DummyScriptWithNonVerboseFlag.fsx create mode 100644 src/FileConventions.Test/DummyFiles/DummyScriptWithoutNonVerboseFlag.fsx diff --git a/src/FileConventions.Test/DummyFiles/DummyScriptWithNonVerboseFlag.fsx b/src/FileConventions.Test/DummyFiles/DummyScriptWithNonVerboseFlag.fsx new file mode 100644 index 00000000..097eeaaa --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyScriptWithNonVerboseFlag.fsx @@ -0,0 +1,17 @@ +#!/usr/bin/env -S dotnet fsi + +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" + +open Fsdk +open Fsdk.Process + +let gitRemote = + { + Command = "git" + Arguments = "remote -v" + } + +let gitRemoteOutput = + Process + .Execute(gitRemote, Echo.All) + .UnwrapDefault() diff --git a/src/FileConventions.Test/DummyFiles/DummyScriptWithoutNonVerboseFlag.fsx b/src/FileConventions.Test/DummyFiles/DummyScriptWithoutNonVerboseFlag.fsx new file mode 100644 index 00000000..6b13300e --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyScriptWithoutNonVerboseFlag.fsx @@ -0,0 +1,17 @@ +#!/usr/bin/env -S dotnet fsi + +#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62" + +open Fsdk +open Fsdk.Process + +let gitRemote = + { + Command = "git" + Arguments = "remote --version" + } + +let gitRemoteOutput = + Process + .Execute(gitRemote, Echo.All) + .UnwrapDefault() diff --git a/src/FileConventions.Test/FileConventions.Test.fs b/src/FileConventions.Test/FileConventions.Test.fs index e39ede58..479ec141 100644 --- a/src/FileConventions.Test/FileConventions.Test.fs +++ b/src/FileConventions.Test/FileConventions.Test.fs @@ -586,3 +586,29 @@ let NonVerboseFlagsInGitHubCI3() = )) Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) + + +[] +let NonVerboseFlagsInGitHubCI4() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyScriptWithNonVerboseFlag.fsx" + ) + )) + + Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo true) + + +[] +let NonVerboseFlagsInGitHubCI5() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyScriptWithoutNonVerboseFlag.fsx" + ) + )) + + Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 1ef21d67..df4da563 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -333,7 +333,10 @@ let DetectInconsistentVersionsInFSharpScripts let allowedNonVerboseFlags = seq { "env -S" } let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = - assert (fileInfo.FullName.EndsWith(".yml")) + assert + (fileInfo.FullName.EndsWith(".yml") + || fileInfo.FullName.EndsWith(".fsx")) + let fileLines = File.ReadLines(fileInfo.FullName) let nonVerboseFlagsRegex = Regex("\\s-[a-zA-Z]\\b", RegexOptions.Compiled) From 885049d742c4510fbaadc13c9291ffd53f1e4cd8 Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 23 Mar 2023 15:57:38 +0330 Subject: [PATCH 06/13] FileConventions: cover .sh and .fs files Cover .sh and .fs files in NonVerboseFlagsInGitHubCI. --- src/FileConventions/Library.fs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index df4da563..cb283383 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -333,9 +333,18 @@ let DetectInconsistentVersionsInFSharpScripts let allowedNonVerboseFlags = seq { "env -S" } let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = + let validExtensions = + seq { + ".yml" + ".fsx" + ".fs" + ".sh" + } + assert - (fileInfo.FullName.EndsWith(".yml") - || fileInfo.FullName.EndsWith(".fsx")) + validExtensions + |> Seq.map(fun ext -> fileInfo.FullName.EndsWith(ext)) + |> Seq.contains true let fileLines = File.ReadLines(fileInfo.FullName) From abfeb7658f2a0d25efa279739788c015ac0fafde Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 23 Mar 2023 14:00:08 +0330 Subject: [PATCH 07/13] scripts: add a script to detect non-verbose flags Detect non-verbose flags in .yml, .sh, .fs and .fsx files. --- .../nonVerboseFlagsInGitHubCIAndScripts.fsx | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx diff --git a/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx b/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx new file mode 100644 index 00000000..ddfe372a --- /dev/null +++ b/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx @@ -0,0 +1,31 @@ +#!/usr/bin/env -S dotnet fsi + +open System +open System.IO + +#load "../src/FileConventions/Library.fs" +#load "../src/FileConventions/Helpers.fs" + +let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo + +let validExtensions = + seq { + ".yml" + ".fsx" + ".fs" + ".sh" + } + +let invalidFiles = + validExtensions + |> Seq.map(fun ext -> + Helpers.GetInvalidFiles + rootDir + ("*" + ext) + FileConventions.NonVerboseFlagsInGitHubCI + ) + |> Seq.concat + +let message = "Please don't use non-verbose flags in the following files:" + +Helpers.AssertNoInvalidFiles invalidFiles message From 3f751c11066693a795760ba354593ef6ecf90757 Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 23 Mar 2023 14:01:52 +0330 Subject: [PATCH 08/13] GitHubCI: run the script Run nonVerboseFlagsInGitHubCIAndFsharpScripts.fsx script and convert non-verbose flags to verbose flags. --- .github/workflows/CI.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index dc1a6f3f..300ef954 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -13,7 +13,7 @@ jobs: - name: Install required dependencies run: | apt update - apt install -y sudo + apt install --yes sudo sudo apt install --yes --no-install-recommends git # workaround for https://github.com/actions/runner/issues/2033 - name: ownership workaround @@ -186,12 +186,14 @@ jobs: run: dotnet fsi scripts/inconsistentVersionsInGitHubCI.fsx - name: Check there are no inconsistent versions in nuget package references of F# scripts run: dotnet fsi scripts/inconsistentVersionsInFSharpScripts.fsx + - name: Check there are no non-verbose flags in scripts and CI YML files + run: dotnet fsi scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx - name: Install prettier run: npm install prettier@2.8.3 - name: Change file permissions # We need this step so we can change the files using `npx prettier --write` in the next step. # Otherwise we get permission denied error in the CI. - run: sudo chmod 777 -R . + run: sudo chmod 777 --recursive . - name: Run "prettier" to check the style of our TypeScript and YML code run: | sudo npx prettier --quote-props=consistent --write './**/*.ts' From baf66fd447a986f254fc2c0c385cbf74276e320c Mon Sep 17 00:00:00 2001 From: realmarv Date: Thu, 23 Mar 2023 14:08:53 +0330 Subject: [PATCH 09/13] ReadMe: update --- ReadMe.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReadMe.md b/ReadMe.md index a9f1621b..709e597f 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -10,6 +10,7 @@ This is a repository that contains several useful things that other `nblockchain * [EOF without EOL detection](scripts/eofConvention.fsx). * [Mixed line-endings detection](scripts/mixedLineEndings.fsx). * [Auto-wrap the latest commit message](scripts/wrapLatestCommitMsg.fsx). + * [Detect non-verbose flags (e.g. `dotnet build -c Debug` instead of `dotnet build --configuration Debug`) being used in scripts or YML CI files (there are exceptions, e.g. `env -S`)](scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx). * Use of unpinned versions: * [Use of `-latest` suffix in `runs-on:` GitHubCI tags](scripts/unpinnedGitHubActionsImageVersions.fsx). * [Use of asterisk (*) in `PackageReference` items of .NET projects](scripts/unpinnedDotnetPackageVersions.fsx). @@ -25,4 +26,3 @@ More things to come: - Detect .fsx files without +x attrib. - Detect old versions of FSharpLint and fantomas/fantomless being used. - Detect old versions of .editorconfig or Directory.Build.props being used. -- Detect non-verbose flags (e.g. `dotnet build -c Debug` instead of `dotnet build --configuration Debug`) being used in scripts or YML CI files (there are exceptions, e.g. `env -S`). From ad5e01d510c00b2f7ab8e721321cc21b9f96dbcb Mon Sep 17 00:00:00 2001 From: realmarv Date: Tue, 4 Jul 2023 14:36:46 +0330 Subject: [PATCH 10/13] FileConventions: failing test for unzip --- ...yml => DummyCIWithAcceptedNonVerboseFlag1.yml} | 0 .../DummyCIWithAcceptedNonVerboseFlag2.yml | 11 +++++++++++ src/FileConventions.Test/FileConventions.Test.fs | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) rename src/FileConventions.Test/DummyFiles/{DummyCIWithAcceptedNonVerboseFlag.yml => DummyCIWithAcceptedNonVerboseFlag1.yml} (100%) create mode 100644 src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag2.yml diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag1.yml similarity index 100% rename from src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag.yml rename to src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag1.yml diff --git a/src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag2.yml b/src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag2.yml new file mode 100644 index 00000000..1a722cfa --- /dev/null +++ b/src/FileConventions.Test/DummyFiles/DummyCIWithAcceptedNonVerboseFlag2.yml @@ -0,0 +1,11 @@ +name: CI + +on: [push, pull_request] + +jobs: + file-conventions: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: unzip a file + run: unzip -n path-to-file.zip diff --git a/src/FileConventions.Test/FileConventions.Test.fs b/src/FileConventions.Test/FileConventions.Test.fs index 479ec141..75b1d506 100644 --- a/src/FileConventions.Test/FileConventions.Test.fs +++ b/src/FileConventions.Test/FileConventions.Test.fs @@ -581,7 +581,7 @@ let NonVerboseFlagsInGitHubCI3() = (FileInfo( Path.Combine( dummyFilesDirectory.FullName, - "DummyCIWithAcceptedNonVerboseFlag.yml" + "DummyCIWithAcceptedNonVerboseFlag1.yml" ) )) @@ -612,3 +612,16 @@ let NonVerboseFlagsInGitHubCI5() = )) Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) + + +[] +let NonVerboseFlagsInGitHubCI6() = + let fileInfo = + (FileInfo( + Path.Combine( + dummyFilesDirectory.FullName, + "DummyCIWithAcceptedNonVerboseFlag2.yml" + ) + )) + + Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) From 5d8681ec85e68bb53ac52ee6091ab2224916b245 Mon Sep 17 00:00:00 2001 From: realmarv Date: Tue, 4 Jul 2023 14:43:53 +0330 Subject: [PATCH 11/13] FileConventions: allow unzip short flags --- src/FileConventions/Library.fs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index cb283383..9edd4291 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -330,7 +330,11 @@ let DetectInconsistentVersionsInFSharpScripts else DetectInconsistentVersionsInNugetRefsInFSharpScripts fsxFiles -let allowedNonVerboseFlags = seq { "env -S" } +let allowedNonVerboseFlags = + seq { + "env -S" + "unzip" + } let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = let validExtensions = From 61d4b002258cd35465bf57f0fc52e8f8fe841e3f Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Wed, 5 Jul 2023 16:08:15 +0800 Subject: [PATCH 12/13] FileConventions: add comment about env -S --- src/FileConventions/Library.fs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 9edd4291..953d9c01 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -332,8 +332,11 @@ let DetectInconsistentVersionsInFSharpScripts let allowedNonVerboseFlags = seq { - "env -S" "unzip" + + // even if env in linux has --split-string=foo as equivalent to env -S, it + // doesn't seem to be present in macOS' env man page and doesn't work either + "env -S" } let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = From be256d4407d4781bfc3940737f7fd4d00d8c9e69 Mon Sep 17 00:00:00 2001 From: realmarv Date: Wed, 5 Jul 2023 17:01:41 +0330 Subject: [PATCH 13/13] FileConventions: refactor --- scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx | 2 +- src/FileConventions.Test/FileConventions.Test.fs | 12 ++++++------ src/FileConventions/Library.fs | 16 +++++++++++----- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx b/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx index ddfe372a..6ffb8801 100644 --- a/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx +++ b/scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx @@ -22,7 +22,7 @@ let invalidFiles = Helpers.GetInvalidFiles rootDir ("*" + ext) - FileConventions.NonVerboseFlagsInGitHubCI + FileConventions.NonVerboseFlags ) |> Seq.concat diff --git a/src/FileConventions.Test/FileConventions.Test.fs b/src/FileConventions.Test/FileConventions.Test.fs index 75b1d506..15ba902e 100644 --- a/src/FileConventions.Test/FileConventions.Test.fs +++ b/src/FileConventions.Test/FileConventions.Test.fs @@ -559,7 +559,7 @@ let NonVerboseFlagsInGitHubCI1() = ) )) - Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo true) + Assert.That(NonVerboseFlags fileInfo, Is.EqualTo true) [] @@ -572,7 +572,7 @@ let NonVerboseFlagsInGitHubCI2() = ) )) - Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) + Assert.That(NonVerboseFlags fileInfo, Is.EqualTo false) [] @@ -585,7 +585,7 @@ let NonVerboseFlagsInGitHubCI3() = ) )) - Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) + Assert.That(NonVerboseFlags fileInfo, Is.EqualTo false) [] @@ -598,7 +598,7 @@ let NonVerboseFlagsInGitHubCI4() = ) )) - Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo true) + Assert.That(NonVerboseFlags fileInfo, Is.EqualTo true) [] @@ -611,7 +611,7 @@ let NonVerboseFlagsInGitHubCI5() = ) )) - Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) + Assert.That(NonVerboseFlags fileInfo, Is.EqualTo false) [] @@ -624,4 +624,4 @@ let NonVerboseFlagsInGitHubCI6() = ) )) - Assert.That(NonVerboseFlagsInGitHubCI fileInfo, Is.EqualTo false) + Assert.That(NonVerboseFlags fileInfo, Is.EqualTo false) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 953d9c01..c5d36f8b 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -339,7 +339,7 @@ let allowedNonVerboseFlags = "env -S" } -let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = +let NonVerboseFlags(fileInfo: FileInfo) = let validExtensions = seq { ".yml" @@ -348,12 +348,18 @@ let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = ".sh" } - assert + let isFileExtentionValid = validExtensions - |> Seq.map(fun ext -> fileInfo.FullName.EndsWith(ext)) + |> Seq.map(fun ext -> fileInfo.FullName.EndsWith ext) |> Seq.contains true - let fileLines = File.ReadLines(fileInfo.FullName) + if not isFileExtentionValid then + let sep = "," + + failwith + $"NonVerboseFlags function only supports {String.concat sep validExtensions} files." + + let fileLines = File.ReadLines fileInfo.FullName let nonVerboseFlagsRegex = Regex("\\s-[a-zA-Z]\\b", RegexOptions.Compiled) @@ -367,7 +373,7 @@ let NonVerboseFlagsInGitHubCI(fileInfo: FileInfo) = |> Seq.map(fun allowedFlag -> line.Contains allowedFlag) |> Seq.contains true - nonVerboseFlag && not(allowedNonVerboseFlag) + nonVerboseFlag && not allowedNonVerboseFlag ) |> Seq.length