Skip to content
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

Easy dotnet conventions to verify #139

Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
50fe215
FileConventions: add failing test
tehraninasab Aug 1, 2023
186213e
FileConventions: implement the function
tehraninasab Aug 1, 2023
2ebd2b5
FileConventions: add failing test
tehraninasab Aug 1, 2023
cc82850
FileConventions: implement the function
tehraninasab Aug 3, 2023
a0272d6
FileConventions: add failing test
tehraninasab Aug 7, 2023
482d4d6
FileConventions: implement the function
tehraninasab Aug 7, 2023
5f06394
FileConventions.Test: add failing test
tehraninasab Aug 7, 2023
12f4d0b
FileConventions: fix the function
tehraninasab Aug 7, 2023
b0acb0c
FileConventions: add failing test
Mersho Aug 17, 2023
7823ab2
FileConventions: fix csharp namespace
Mersho Aug 17, 2023
9b8afb3
FileConventions: add failing test
Mersho Aug 17, 2023
21f3749
FileConventions: `Contains()` is not suitable here
Mersho Aug 17, 2023
81bfb57
FileConventions: use BetterAssert() & update Fsdk
Mersho Aug 21, 2023
e124a91
FileConventions(.Test): applying f# standard style
Mersho Aug 21, 2023
eb5f76c
FileConventions: add failing test
Mersho Aug 21, 2023
885a60a
FileConventions: fix the function
Mersho Aug 21, 2023
4e0c6e1
FileConventions: add failing test
Mersho Aug 22, 2023
6e80056
FileConvention: fix the function
Mersho Aug 22, 2023
46f3dae
FileConventions: add failing test
Mersho Aug 22, 2023
45e298b
FileConvention: fix the function
Mersho Aug 22, 2023
c190f9c
FileConventions(.Test): fix proj file indent
Mersho Aug 28, 2023
d49a99c
FileConvention: properly naming variables
Mersho Aug 28, 2023
19b442d
FileConventions: add failing test
Mersho Aug 29, 2023
e6fc879
FileConvention: fix the function
Mersho Aug 29, 2023
5358b96
FileConventions.Test: add failing test
Mersho Aug 23, 2023
ce4c0c6
FileConventions: fix empty string false-positives
Mersho Aug 23, 2023
18b828e
FileConventions: load Helpers.fs first
Mersho Aug 24, 2023
1fd7576
FileConventions: improvements to Library.fs
Mersho Aug 24, 2023
a326d09
FileConventions: dotnetFileConvention
Mersho Aug 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env -S dotnet fsi

open System
open System.IO

let emptyString = String.Empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env -S dotnet fsi

open System
open System.IO

let emptyString = ""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
namespace Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
namespace Baz
84 changes: 84 additions & 0 deletions src/FileConventions.Test/FileConventions.Test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,87 @@ let IsExecutableTest2() =
))

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


[<Test>]
let DefiningEmptyStringsWithDoubleQuotes1() =
let fileInfo =
(FileInfo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra parens

Path.Combine(
dummyFilesDirectory.FullName,
"DummyScriptWithConventionalEmptyString.fsx"
)
))

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


[<Test>]
let DefiningEmptyStringsWithDoubleQuotes2() =
let fileInfo =
(FileInfo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra parens

Path.Combine(
dummyFilesDirectory.FullName,
"DummyScriptWithNonConventionalEmptyString.fsx"
)
))

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


[<Test>]
let ProjFilesNamingConvention1() =
let fileInfo =
(FileInfo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra paren

Path.Combine(
dummyFilesDirectory.FullName,
"DummyProjFileWithTheSameNameAsItsParentFolder",
"DummyProjFileWithTheSameNameAsItsParentFolder.fsproj"
)
))

Assert.That(ProjFilesNamingConvention fileInfo, Is.EqualTo false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert.False?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope



[<Test>]
let ProjFilesNamingConvention2() =
let fileInfo =
(FileInfo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra paren.

Path.Combine(
dummyFilesDirectory.FullName,
"DummyProject",
"DummyProjFileWithoutTheSameNameAsItsParentFolder.fsproj"
)
))

Assert.That(ProjFilesNamingConvention fileInfo, Is.EqualTo true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it easier to use Assert.True?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, Assert.That is fine



[<Test>]
let NamespaceConvention1() =
let fileInfo =
(FileInfo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra paren

Path.Combine(
dummyFilesDirectory.FullName,
"src",
"Foo",
"DummyFileUnderFooWithRightNamespace.fs"
)
))

Assert.That(NotFollowingNamespaceConvention fileInfo, Is.EqualTo false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert.False?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope



[<Test>]
let NamespaceConvention2() =
let fileInfo =
(FileInfo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra paren

Path.Combine(
dummyFilesDirectory.FullName,
"src",
"Foo",
"DummyFileUnderFooWithWrongNamespace.fs"
)
))

Assert.That(NotFollowingNamespaceConvention fileInfo, Is.EqualTo true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert.True?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope

49 changes: 49 additions & 0 deletions src/FileConventions/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,52 @@ let NonVerboseFlags(fileInfo: FileInfo) =
let IsExecutable(fileInfo: FileInfo) =
let hasExecuteAccess = Syscall.access(fileInfo.FullName, AccessModes.X_OK)
hasExecuteAccess = 0

let DefiningEmptyStringsWithDoubleQuotes(fileInfo: FileInfo) =
let fileText = File.ReadAllText fileInfo.FullName
fileText.Contains "\"\""

let ProjFilesNamingConvention(fileInfo: FileInfo) =
let regex = new Regex("(.*)\..*proj$")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for new keyword

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also extra paren.

assert (regex.IsMatch fileInfo.FullName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about this but I think paren is not needed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is needed; because assert doesn't receive 2 currified arguments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert is for debug mode only, are we sure it should be used here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is needed; because assert doesn't receive 2 currified arguments

Not sure because it compiles without them 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aarani you're right, they should use Fsdk's Misc.BetterAssert

let fileName = Path.GetFileNameWithoutExtension fileInfo.FullName

let parentDirectoryName =
Path.GetDirectoryName fileInfo.FullName |> Path.GetFileName

printfn
"File name: %s, Parent directory name: %s"
fileName
parentDirectoryName

fileName <> parentDirectoryName

let NotFollowingNamespaceConvention(fileInfo: FileInfo) =
assert (fileInfo.FullName.EndsWith(".fs"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra paren


let fileName = Path.GetFileNameWithoutExtension fileInfo.FullName

let parentDirectoryName =
Path.GetDirectoryName fileInfo.FullName |> Path.GetFileName

printfn
"File name: %s, Parent directory name: %s"
fileName
parentDirectoryName

if parentDirectoryName <> "src"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the logic is based on the corresponding issue, but for instance in NX we have multiple projects in "tests" folder that this rule can be applied to them. Can't we modify the rule to search for the folders containing projects?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

later

&& fileInfo.FullName.Contains
$"{Path.DirectorySeparatorChar}src{Path.DirectorySeparatorChar}" then
let fileText = File.ReadLines fileInfo.FullName

if fileText.Any() then
let firstLine = fileText.First()

if firstLine.Contains "namespace" then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not something like this?

firstLine.Contains "namespace" && (firstLine.Contains parentDirectoryName |> not)

firstLine.Contains parentDirectoryName |> not
else
false
else
false
else
false
Loading