Skip to content

Commit 8d0139d

Browse files
committed
Add check for missing readme in library
1 parent db3526b commit 8d0139d

File tree

8 files changed

+75
-0
lines changed

8 files changed

+75
-0
lines changed

check/checkconfigurations/checkconfigurations.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,21 @@ var configurations = []Type{
956956
ErrorModes: []checkmode.Type{checkmode.Default},
957957
CheckFunction: checkfunctions.ProhibitedCharactersInLibraryFolderName,
958958
},
959+
{
960+
ProjectType: projecttype.Library,
961+
Category: "structure",
962+
Subcategory: "",
963+
ID: "",
964+
Brief: "no readme",
965+
Description: "",
966+
MessageTemplate: "No readme found. Please document your library. See: https://docs.github.com/en/free-pro-team@latest/github/creating-cloning-and-archiving-repositories/about-readmes",
967+
DisableModes: nil,
968+
EnableModes: []checkmode.Type{checkmode.Default},
969+
InfoModes: nil,
970+
WarningModes: []checkmode.Type{checkmode.Default},
971+
ErrorModes: []checkmode.Type{checkmode.Strict},
972+
CheckFunction: checkfunctions.MissingReadme,
973+
},
959974
{
960975
ProjectType: projecttype.Sketch,
961976
Category: "structure",

check/checkfunctions/checkfunctions.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"regexp"
2121
"strings"
2222

23+
"github.com/arduino/arduino-check/check/checkdata"
2324
"github.com/arduino/arduino-check/check/checkresult"
2425
"github.com/arduino/go-paths-helper"
2526
)
@@ -64,3 +65,35 @@ func containsIncorrectPathBaseCase(pathList paths.PathList, correctBaseName stri
6465

6566
return nil, false
6667
}
68+
69+
// MissingReadme checks if the project has a readme that will be recognized by GitHub.
70+
func MissingReadme() (result checkresult.Type, output string) {
71+
// https://github.com/github/markup/blob/master/README.md
72+
readmeRegexp := regexp.MustCompile(`(?i)^readme\.(markdown)|(mdown)|(mkdn)|(md)|(textile)|(rdoc)|(org)|(creole)|(mediawiki)|(wiki)|(rst)|(asciidoc)|(adoc)|(asc)|(pod)|(txt)`)
73+
74+
// https://docs.github.com/en/free-pro-team@latest/github/creating-cloning-and-archiving-repositories/about-readmes#about-readmes
75+
if pathContainsReadme(checkdata.ProjectPath(), readmeRegexp) ||
76+
(checkdata.ProjectPath().Join("docs").Exist() && pathContainsReadme(checkdata.ProjectPath().Join("docs"), readmeRegexp)) ||
77+
(checkdata.ProjectPath().Join(".github").Exist() && pathContainsReadme(checkdata.ProjectPath().Join(".github"), readmeRegexp)) {
78+
return checkresult.Pass, ""
79+
}
80+
81+
return checkresult.Fail, ""
82+
}
83+
84+
// pathContainsReadme checks if the provided path contains a readme file recognized by GitHub.
85+
func pathContainsReadme(path *paths.Path, readmeRegexp *regexp.Regexp) bool {
86+
listing, err := path.ReadDir()
87+
if err != nil {
88+
panic(err)
89+
}
90+
listing.FilterOutDirs()
91+
92+
for _, file := range listing {
93+
if readmeRegexp.MatchString(file.Base()) {
94+
return true
95+
}
96+
}
97+
98+
return false
99+
}

check/checkfunctions/library_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,12 @@ func TestRecursiveLibraryWithUtilityFolder(t *testing.T) {
381381

382382
checkLibraryCheckFunction(RecursiveLibraryWithUtilityFolder, testTables, t)
383383
}
384+
385+
func TestMissingReadme(t *testing.T) {
386+
testTables := []libraryCheckFunctionTestTable{
387+
{"Readme", "Readme", checkresult.Pass, ""},
388+
{"No readme", "NoReadme", checkresult.Fail, ""},
389+
}
390+
391+
checkLibraryCheckFunction(MissingReadme, testTables, t)
392+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=NoReadme
2+
version=1.0.0
3+
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
4+
maintainer=Cristian Maglie <c.maglie@example.com>
5+
sentence=A library that makes coding a web server a breeze.
6+
paragraph=Supports HTTP1.1 and you can do GET and POST.
7+
category=Communication
8+
url=http://example.com/
9+
architectures=avr

check/checkfunctions/testdata/libraries/NoReadme/src/NoReadme.h

Whitespace-only changes.

check/checkfunctions/testdata/libraries/Readme/README.md

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Readme
2+
version=1.0.0
3+
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
4+
maintainer=Cristian Maglie <c.maglie@example.com>
5+
sentence=A library that makes coding a web server a breeze.
6+
paragraph=Supports HTTP1.1 and you can do GET and POST.
7+
category=Communication
8+
url=http://example.com/
9+
architectures=avr

check/checkfunctions/testdata/libraries/Readme/src/Readme.h

Whitespace-only changes.

0 commit comments

Comments
 (0)