Skip to content

Improve DetectUnpinnedDotnetToolInstallVersions function #189

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
npm --version
- name: Install yarn
run: |
npm install --verbose --global yarn
npm install --verbose --global yarn@1.22.19
yarn add --dev typescript
- name: Install commitlint dependencies
run: |
Expand Down Expand Up @@ -121,7 +121,7 @@ jobs:
sudo apt install --yes --no-install-recommends npm curl
- name: Install yarn
run: |
npm install --verbose --global yarn
npm install --verbose --global yarn@1.22.19
yarn add --dev typescript ts-node
- name: Install commitlint dependencies
run: |
Expand Down Expand Up @@ -210,6 +210,8 @@ jobs:
run: dotnet fsi scripts/unpinnedNugetPackageReferenceVersionsInFSharpScripts.fsx
- name: Check there are no unpinned versions in `dotnet tool install` commands
run: dotnet fsi scripts/unpinnedDotnetToolInstallVersions.fsx
- name: Check there are no unpinned versions in `npm install` commands
run: dotnet fsi scripts/unpinnedNpmPackageInstallVersions.fsx
- name: Check commits 1 by 1
if: github.event_name == 'pull_request'
run: dotnet fsi scripts/checkCommits1by1.fsx
Expand Down
23 changes: 23 additions & 0 deletions scripts/unpinnedNpmPackageInstallVersions.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env -S dotnet fsi

open System
open System.IO

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

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

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

let invalidFiles =
Helpers.GetInvalidFiles
rootDir
"*.yml"
FileConventions.DetectUnpinnedNpmPackageInstallVersions

let message =
"Please define the package version number in the `npm install` commands."

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: Install prettier without specifying its version
run: npm install prettier
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: Install an npm package without specifying its version
run: sudo npm install --save-dev @prettier/plugin-xml
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: Install two npm packages, one with version and the other one without version
run: sudo npm install --save-dev @prettier/plugin-xml prettier@2.4.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI

on: [push, pull_request]

jobs:
build:
name: Build
runs-on: ubuntu-22.04
container:
image: "ubuntu:22.04"
steps:
- name: Install fantomless-tool
run: |
dotnet tool install fantomless-tool --version 4.8.999
- name: Print "Hello World!"
run: 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: Install prettier with specifying its version
run: npm install prettier@2.8.3
79 changes: 79 additions & 0 deletions src/FileConventions.Test/FileConventions.Test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,85 @@ let DetectUnpinnedDotnetToolInstallVersions1() =
Is.EqualTo true
)

[<Test>]
let DetectUnpinnedDotnetToolInstallVersions2() =
let fileInfo =
(FileInfo(
Path.Combine(
dummyFilesDirectory.FullName,
"DummyCIWithoutUnpinnedDotnetToolInstallVersion.yml"
)
))

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


[<Test>]
let DetectUnpinnedNpmPackageInstallVersions1() =
let fileInfo =
(FileInfo(
Path.Combine(
dummyFilesDirectory.FullName,
"DummyCIWithUnpinnedNpmPackageInstallVersion1.yml"
)
))

Assert.That(
DetectUnpinnedNpmPackageInstallVersions fileInfo,
Is.EqualTo true
)


[<Test>]
let DetectUnpinnedNpmPackageInstallVersions2() =
let fileInfo =
(FileInfo(
Path.Combine(
dummyFilesDirectory.FullName,
"DummyCIWithoutUnpinnedNpmPackageInstallVersion.yml"
)
))

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


[<Test>]
let DetectUnpinnedNpmPackageInstallVersions3() =
let fileInfo =
(FileInfo(
Path.Combine(
dummyFilesDirectory.FullName,
"DummyCIWithUnpinnedNpmPackageInstallVersion2.yml"
)
))

Assert.That(
DetectUnpinnedNpmPackageInstallVersions fileInfo,
Is.EqualTo true
)


[<Test>]
let DetectUnpinnedNpmPackageInstallVersions4() =
let fileInfo =
(FileInfo(
Path.Combine(
dummyFilesDirectory.FullName,
"DummyCIWithUnpinnedNpmPackageInstallVersion3.yml"
)
))

Assert.That(
DetectUnpinnedNpmPackageInstallVersions fileInfo,
Is.EqualTo true
)


[<Test>]
let DetectAsteriskInPackageReferenceItems1() =
Expand Down
38 changes: 36 additions & 2 deletions src/FileConventions/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ let DetectUnpinnedVersionsInGitHubCI(fileInfo: FileInfo) =
latestTagInRunsOnRegex.IsMatch fileText

let DetectUnpinnedDotnetToolInstallVersions(fileInfo: FileInfo) =
assert (fileInfo.FullName.EndsWith(".yml"))
assert fileInfo.FullName.EndsWith ".yml"

let fileLines = File.ReadLines fileInfo.FullName

Expand All @@ -84,12 +84,46 @@ let DetectUnpinnedDotnetToolInstallVersions(fileInfo: FileInfo) =
fileLines
|> Seq.filter dotnetToolInstallRegex.IsMatch
|> Seq.filter(fun line ->
not(line.Contains("--version")) && not(line.Contains("-v"))
not(line.Contains "--version") && not(line.Contains "-v")
)
|> (fun unpinnedVersions -> Seq.length unpinnedVersions > 0)

unpinnedDotnetToolInstallVersions

let DetectUnpinnedNpmPackageInstallVersions(fileInfo: FileInfo) =
assert fileInfo.FullName.EndsWith ".yml"

let fileLines = File.ReadLines fileInfo.FullName

let npmPackageInstallRegex =
Regex("npm\\s+install\\s+", RegexOptions.Compiled)

let npmPackageVersionRegex =
Regex("@((\\d+\\.\\d+\\.\\d+)|(\\$[A-Z_]+))", RegexOptions.Compiled)

let unpinnedNpmPackageInstallVersions =
fileLines
|> Seq.filter(fun line -> npmPackageInstallRegex.IsMatch line)
|> Seq.filter(fun line ->
let npmPackagesRegex =
Regex("(?<=npm install ).*$", RegexOptions.Compiled)

let npmInstallPackages = npmPackagesRegex.Match line

let numNpmInstallPackages =
npmInstallPackages.Value.Split(" ")
|> Seq.filter(fun word -> word.Trim().StartsWith("-") |> not)
|> Seq.length

let numNpmInstallVersions =
npmPackageVersionRegex.Matches line |> Seq.length

numNpmInstallPackages = numNpmInstallVersions |> not
)
|> (fun unpinnedVersions -> Seq.length unpinnedVersions > 0)

unpinnedNpmPackageInstallVersions

let DetectAsteriskInPackageReferenceItems(fileInfo: FileInfo) =
assert (fileInfo.FullName.EndsWith "proj")

Expand Down