-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect non-verbose flags being used in F# scripts or YML CI files #91
Changes from all commits
0236c98
76c2083
840a2fa
4d67423
a6ee086
885049d
abfeb76
3f751c1
baf66fd
ad5e01d
5d8681e
61d4b00
be256d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 [email protected] | ||
- 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' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.NonVerboseFlags | ||
) | ||
|> Seq.concat | ||
|
||
let message = "Please don't use non-verbose flags in the following files:" | ||
|
||
Helpers.AssertNoInvalidFiles invalidFiles message |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -329,3 +329,52 @@ let DetectInconsistentVersionsInFSharpScripts | |
false | ||
else | ||
DetectInconsistentVersionsInNugetRefsInFSharpScripts fsxFiles | ||
|
||
let allowedNonVerboseFlags = | ||
seq { | ||
"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 NonVerboseFlags(fileInfo: FileInfo) = | ||
let validExtensions = | ||
seq { | ||
".yml" | ||
".fsx" | ||
".fs" | ||
".sh" | ||
} | ||
|
||
let isFileExtentionValid = | ||
validExtensions | ||
|> Seq.map(fun ext -> fileInfo.FullName.EndsWith ext) | ||
|> Seq.contains true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @realmarv can you explain what exactly are you doing here please? this is a bit unreadable; also, why are you using assert instead of failwith? I'm actually not familiar with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @realmarv BTW I just cooked this in case you want to use it: nblockchain/fsx@7acb7a5 (will be available in nuget in 5min) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It loops over validExtensions sequence and checks if the file path ends with any of them |
||
|
||
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) | ||
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@realmarv aren't you missing a dot after * here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No ext already has a dot