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

lint: add initial checks for approaches and articles #704

Merged
merged 13 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
25 changes: 17 additions & 8 deletions src/lint/approaches_and_articles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ proc isConfigMissingOrValid(dir: Path, dk: DirKind): bool =
if not isValidConfig(j, configPath, dk):
result = false

func countLinesWithoutCodeFence(s: string, dk: DirKind): int =
## Returns the number of lines in `s`, but:
##
## - excluding lines that open or close a Markdown code fence.
## - including a final line that does not end in a newline character.
result = 0
if s.len > 0:
for line in s.splitLines():
if not (line.startsWith("```") and dk == dkArticles):
Copy link
Member Author

Choose a reason for hiding this comment

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

Does not handle the fact that a code fence can begin with ~~~

Copy link
Member

Choose a reason for hiding this comment

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

That's probably perfectly fine

inc result
if s[^1] in ['\n', '\l']:
dec result

proc isEverySnippetValid(exerciseDir: Path, dk: DirKind): bool =
result = true
for dir in getSortedSubdirs(exerciseDir / $dk):
Expand All @@ -57,15 +70,11 @@ proc isEverySnippetValid(exerciseDir: Path, dk: DirKind): bool =
dir / &"snippet.{ext}"
if fileExists(snippetPath):
let contents = readFile(snippetPath)
var numLines = 0
for line in contents.splitLines():
if not (line.startsWith("```") and dk == dkArticles):
inc numLines
dec numLines # Allow 8 lines with a final newline.
const maxNumLines = 8
if numLines > maxNumLines:
const maxLines = 8
let numLines = countLinesWithoutCodeFence(contents, dk)
if numLines > maxLines:
let msg = &"The file is {numLines} lines long, but it must be at " &
&"most {maxNumLines} lines long"
&"most {maxLines} lines long"
result.setFalseAndPrint(msg, snippetPath)

proc isEveryApproachAndArticleValid*(trackDir: Path): bool =
Expand Down
69 changes: 68 additions & 1 deletion tests/test_lint.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import std/unittest
import std/[strutils, unittest]
import "."/lint/validators
import "."/lint/approaches_and_articles {.all.}

proc testIsKebabCase =
suite "isKebabCase":
Expand Down Expand Up @@ -279,11 +280,77 @@ proc testIsFilesPattern =
isFilesPattern("somedir/%{pascal_slug}/%{snake_slug}.suffix")
isFilesPattern("%{pascal_slug}/filename.suffix")

proc testCountLinesWithoutCodeFence =
suite "countLinesWithoutCodeFence":
test "without code fence":
const dk = dkArticles
check:
countLinesWithoutCodeFence("", dk) == 0
countLinesWithoutCodeFence("a", dk) == 1
countLinesWithoutCodeFence("a\n", dk) == 1
countLinesWithoutCodeFence("foo\n", dk) == 1
countLinesWithoutCodeFence("foo\nb", dk) == 2
countLinesWithoutCodeFence("foo\nbar", dk) == 2
countLinesWithoutCodeFence("foo\nbar\n", dk) == 2
countLinesWithoutCodeFence("foo\nbar\nfoo", dk) == 3

test "with code fence only":
const s = """
```nim
echo "foo"
```""".unindent()
check:
countLinesWithoutCodeFence(s, dkArticles) == 1
countLinesWithoutCodeFence(s, dkApproaches) == 3

test "with code fence at start":
const s = """
```nim
echo "foo"
```
Some content.
""".unindent()
check:
countLinesWithoutCodeFence(s, dkArticles) == 3
countLinesWithoutCodeFence(s, dkApproaches) == 5

test "with code fence at end":
const s = """
# Some header
Some content.
```nim
echo "foo"
```
""".unindent()
check:
countLinesWithoutCodeFence(s, dkArticles) == 5
countLinesWithoutCodeFence(s, dkApproaches) == 7

test "with code fence in middle":
const s = """
# Some header
Some content.
```nim
echo "foo"
```
Some content.
""".unindent()
check:
countLinesWithoutCodeFence(s, dkArticles) == 7
countLinesWithoutCodeFence(s, dkApproaches) == 9

proc main =
testIsKebabCase()
testIsUuidV4()
testExtractPlaceholders()
testIsFilesPattern()
testCountLinesWithoutCodeFence()

main()
{.used.}