Skip to content

Commit

Permalink
Merge PR nblockchain#73 from realmarv/addIsExecutableFunctionToFileCo…
Browse files Browse the repository at this point in the history
…nventions

New executableConvention fsx script that flags when an .fsx
file doesn't have the executable +x flag.
  • Loading branch information
knocte authored Aug 8, 2023
2 parents b2965b5 + fdd0255 commit a096b47
Show file tree
Hide file tree
Showing 22 changed files with 95 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ jobs:
run: dotnet fsi scripts/eofConvention.fsx
- name: Check all .fsx scripts have shebang
run: dotnet fsi scripts/shebangConvention.fsx
- name: Check all F# scripts have execute permission
run: dotnet fsi scripts/executableConvention.fsx
- name: Check there are no mixed line-endings in any files
run: dotnet fsi scripts/mixedLineEndings.fsx
- name: Check there are no unpinned GitHubActions image versions
Expand Down
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This is a repository that contains several useful things that other `nblockchain
- [Workflow guidelines](docs/WorkflowGuidelines.md).
- Scripts that aid maintainability:
* [Detection of .fsx scripts without shebang](scripts/shebangConvention.fsx).
* [Detection of .fsx files without +x attrib](scripts/executableConvention.fsx).
* [F# scripts compilation](scripts/compileFSharpScripts.fsx).
* [EOF without EOL detection](scripts/eofConvention.fsx).
* [Mixed line-endings detection](scripts/mixedLineEndings.fsx).
Expand All @@ -23,6 +24,5 @@ This is a repository that contains several useful things that other `nblockchain
All in all, this is mainly documentation, and some tooling to detect bad practices.

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.
Empty file modified scripts/checkCommits1by1.fsx
100644 → 100755
Empty file.
Empty file modified scripts/compileFSharpScripts.fsx
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions scripts/eofConvention.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
open System.IO
open System

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Helpers.fs"
#load "../src/FileConventions/Library.fs"

Expand Down
20 changes: 20 additions & 0 deletions scripts/executableConvention.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env -S dotnet fsi

open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"
#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo

let invalidFiles =
Helpers.GetInvalidFiles
rootDir
"*.fsx"
(fun fileInfo -> not(FileConventions.IsExecutable fileInfo))

Helpers.AssertNoInvalidFiles
invalidFiles
"The following files don't have execute permission:"
2 changes: 2 additions & 0 deletions scripts/inconsistentVersionsInFSharpScripts.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
open System.IO
open System.Linq

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 2 additions & 0 deletions scripts/inconsistentVersionsInGitHubCI.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 2 additions & 0 deletions scripts/mixedLineEndings.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 2 additions & 0 deletions scripts/nonVerboseFlagsInGitHubCIAndScripts.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 2 additions & 0 deletions scripts/shebangConvention.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 2 additions & 0 deletions scripts/unpinnedDotnetPackageVersions.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 2 additions & 0 deletions scripts/unpinnedDotnetToolInstallVersions.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 2 additions & 0 deletions scripts/unpinnedGitHubActionsImageVersions.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 2 additions & 0 deletions scripts/unpinnedNugetPackageReferenceVersions.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
open System
open System.IO

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"
#load "../src/FileConventions/Helpers.fs"

Expand Down
2 changes: 2 additions & 0 deletions scripts/wrapLatestCommitMsg.fsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ open System.IO
open System
open System.Text.RegularExpressions

#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1"

#load "../src/FileConventions/Library.fs"

#r "nuget: Fsdk, Version=0.6.0--date20230214-0422.git-1ea6f62"
Expand Down
Empty file.
Empty file.
37 changes: 37 additions & 0 deletions src/FileConventions.Test/FileConventions.Test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ open System.IO

open NUnit.Framework
open NUnit.Framework.Constraints
open Fsdk
open Fsdk.Process

open FileConventions

Expand Down Expand Up @@ -549,3 +551,38 @@ let NonVerboseFlagsInGitHubCI6() =
))

Assert.That(NonVerboseFlags fileInfo, Is.EqualTo false)


[<Test>]
let IsExecutableTest1() =
let filePath =
Path.Combine(__SOURCE_DIRECTORY__, "DummyFiles", "DummyExecutable.fsx")

Fsdk
.Process
.Execute(
{
Command = "chmod"
Arguments = sprintf "+x %s" filePath
},
Echo.All
)
.UnwrapDefault()
|> ignore<string>

let fileInfo = (FileInfo filePath)
Assert.That(IsExecutable fileInfo, Is.EqualTo true)


[<Test>]
let IsExecutableTest2() =
let fileInfo =
(FileInfo(
Path.Combine(
__SOURCE_DIRECTORY__,
"DummyFiles",
"DummyNotExecutable.fs"
)
))

Assert.That(IsExecutable fileInfo, Is.EqualTo false)
1 change: 1 addition & 0 deletions src/FileConventions.Test/FileConventions.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Fsdk" Version="0.6.0--date20230214-0422.git-1ea6f62" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/FileConventions/FileConventions.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
<Compile Include="Helpers.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Mono.Posix" Version="7.1.0-final.1.21458.1" />
<PackageReference Include="Mono.Unix" Version="7.1.0-final.1.21458.1" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/FileConventions/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ open System.IO
open System.Linq
open System.Text.RegularExpressions

open Mono
open Mono.Unix.Native

let HasCorrectShebang(fileInfo: FileInfo) =
let fileText = File.ReadLines fileInfo.FullName

Expand Down Expand Up @@ -383,3 +386,7 @@ let NonVerboseFlags(fileInfo: FileInfo) =
|> Seq.length

numInvalidFlags > 0

let IsExecutable(fileInfo: FileInfo) =
let hasExecuteAccess = Syscall.access(fileInfo.FullName, AccessModes.X_OK)
hasExecuteAccess = 0

0 comments on commit a096b47

Please sign in to comment.