Skip to content

Commit

Permalink
Allows checks to supply a default config, and does so for "testing" […
Browse files Browse the repository at this point in the history
…fix]
  • Loading branch information
hoijui committed Dec 3, 2023
1 parent a7a85a8 commit 9a00be1
Show file tree
Hide file tree
Showing 22 changed files with 54 additions and 40 deletions.
12 changes: 11 additions & 1 deletion src/check.nim
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,22 @@ method configSchema*(this: CheckGenerator): Option[JsonNode] {.base.} =
## of the type of check generated by this.
return none[JsonNode]()

method defaultConfigJson*(this: CheckGenerator): Option[JsonNode] {.base.} =
## Returns the default JSON config for the check type.
return none[JsonNode]()

method defaultConfig*(this: CheckGenerator): CheckConfig {.base.} =
## Returns the default config for the check type.
let config = newCheckConfig(this.id())
config.json = this.defaultConfigJson()
return config

method isEnabled*(this: CheckGenerator): bool {.base.} =
## Returns whether this check is enabled by default.
## This should only be set to false for unstable and testing checks.
return true

method generate*(this: CheckGenerator, config: CheckConfig = newCheckConfig(this.id())): Check {.base.} =
method generate*(this: CheckGenerator, config: CheckConfig = this.defaultConfig()): Check {.base.} =
## Generates a check instance,
## using either the default configuration if `none` is supplied,
## or configured by the JSON formatted configuration given.
Expand Down
2 changes: 1 addition & 1 deletion src/checker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ proc check*(registry: var ChecksRegistry, state: var State) =
var maxScoreSum = CheckSignificance()
var scoreSum = CheckSignificance()
let checksConfigs = state.config.checks
let allChecks = registry.getChecks(some(checksConfigs))
let allChecks = registry.getChecks(checksConfigs)
let numChecks = len(allChecks)
for primaryId, check in allChecks:
let res = check.run(state)
Expand Down
2 changes: 1 addition & 1 deletion src/checks/bom_exists.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Please consider adding e.g. a 'BoM.csv'.""")
method id*(this: BomExistsCheckGenerator): string =
return ID

method generate*(this: BomExistsCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: BomExistsCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
BomExistsCheck()

Expand Down
3 changes: 1 addition & 2 deletions src/checks/clean_cad_files.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#
# SPDX-License-Identifier: AGPL-3.0-or-later

import options
import os
import std/tables
import system
Expand Down Expand Up @@ -79,7 +78,7 @@ method run*(this: CleanCadFilesCheck, state: var State): CheckResult =
method id*(this: CleanCadFilesCheckGenerator): string =
return ID

method generate*(this: CleanCadFilesCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: CleanCadFilesCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
CleanCadFilesCheck()

Expand Down
3 changes: 1 addition & 2 deletions src/checks/clean_electronics_files.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#
# SPDX-License-Identifier: AGPL-3.0-or-later

import options
import os
import std/tables
import system
Expand Down Expand Up @@ -79,7 +78,7 @@ method run*(this: CleanElectronicsFilesCheck, state: var State): CheckResult =
method id*(this: CleanElectronicsFilesCheckGenerator): string =
return ID

method generate*(this: CleanElectronicsFilesCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: CleanElectronicsFilesCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
CleanElectronicsFilesCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/license_exists.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ method run*(this: LicenseExistsCheck, state: var State): CheckResult =
method id*(this: LicenseExistsCheckGenerator): string =
return ID

method generate*(this: LicenseExistsCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: LicenseExistsCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
LicenseExistsCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/markdown_lint.nim
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ method run*(this: MarkdownLintCheck, state: var State): CheckResult =
method id*(this: MarkdownLintCheckGenerator): string =
return ID

method generate*(this: MarkdownLintCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: MarkdownLintCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
MarkdownLintCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/markup_link.nim
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ method run*(this: MarkupLinkCheck, state: var State): CheckResult =
method id*(this: MarkupLinkCheckGenerator): string =
return ID

method generate*(this: MarkupLinkCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: MarkupLinkCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
MarkupLinkCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/no_generated_files.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ method run*(this: NoGeneratedFilesCheck, state: var State): CheckResult =
method id*(this: NoGeneratedFilesCheckGenerator): string =
return ID

method generate*(this: NoGeneratedFilesCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: NoGeneratedFilesCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
NoGeneratedFilesCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/no_global_links_to_local_files.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ method run*(this: MdNoGlobalLinksToLocalFilesCheck, state: var State): CheckResu
method id*(this: MdNoGlobalLinksToLocalFilesCheckGenerator): string =
return ID

method generate*(this: MdNoGlobalLinksToLocalFilesCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: MdNoGlobalLinksToLocalFilesCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
MdNoGlobalLinksToLocalFilesCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/no_sources_in_root.nim
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ method run*(this: NoSourceFilesInRootCheck, state: var State): CheckResult =
method id*(this: NoSourceFilesInRootCheckGenerator): string =
return ID

method generate*(this: NoSourceFilesInRootCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: NoSourceFilesInRootCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
NoSourceFilesInRootCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/no_space_in_file_names.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ method run*(this: NoSpaceInFileNamesCheck, state: var State): CheckResult =
method id*(this: NoSpaceInFileNamesCheckGenerator): string =
return ID

method generate*(this: NoSpaceInFileNamesCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: NoSpaceInFileNamesCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
NoSpaceInFileNamesCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/okh_file_exists.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ method run*(this: OkhFileExistsCheck, state: var State): CheckResult =
method id*(this: OkhFileExistsCheckGenerator): string =
return ID

method generate*(this: OkhFileExistsCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: OkhFileExistsCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
OkhFileExistsCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/okh_lint.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ method run*(this: OkhLintCheck, state: var State): CheckResult =
method id*(this: OkhLintCheckGenerator): string =
return ID

method generate*(this: OkhLintCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: OkhLintCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
OkhLintCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/readme_exists.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ method run(this: ReadmeExistsCheck, state: var State): CheckResult =
method id*(this: ReadmeExistsCheckGenerator): string =
return ID

method generate*(this: ReadmeExistsCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: ReadmeExistsCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
ReadmeExistsCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/reuse_lint.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ method run*(this: ReuseLintCheck, state: var State): CheckResult =
method id*(this: ReuseLintCheckGenerator): string =
return ID

method generate*(this: ReuseLintCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: ReuseLintCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
ReuseLintCheck()

Expand Down
30 changes: 20 additions & 10 deletions src/checks/testing.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import options
import strformat
import std/json
import std/jsonutils
import tables
import ../check
import ../check_config
import ../invalid_config_exception
import ../state
import ../util/fs

Expand Down Expand Up @@ -68,6 +66,15 @@ method getSignificanceFactors*(this: TestingCheck): CheckSignificance =
method configSchema*(this: TestingCheckGenerator): Option[JsonNode] =
return some(json.parseJson(CONFIG_JSON_SCHEMA))

method defaultConfigJson*(this: TestingCheckGenerator): Option[JsonNode] =
## Returns the default JSON config for teh check type.
# return json.parseJson("{}")
return some(json.parseJson("""
{
"pass": false
}
"""))

# TODO Remove this proc entirely, when copy&pasting this file, to use it as a template for your own check
method isEnabled*(this: TestingCheckGenerator): bool =
return false
Expand All @@ -89,14 +96,17 @@ method run*(this: TestingCheck, state: var State): CheckResult =
method id*(this: TestingCheckGenerator): string =
return ID

method generate*(this: TestingCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
if config.json.isNone():
raise InvalidConfigException.newException(
fmt"This check ({this.id()}) requires a configuration to be set")
let jsonConfig = jsonutils.toJson(config.json.get())
if not jsonConfig.contains("pass"):
raise InvalidConfigException.newException(
fmt"This check ({this.id()}) requires the ocnfig property 'pass' (boolean) to be set")
method generate*(this: TestingCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
let jsonConfig = if config.json.isSome():
config.json.get()
else:
this.defaultConfigJson().get()
# NOTE We need not check this here,
# because the JSON Schema validation already takes care of that.
# Though, we leave it here for docu purposes.
# if not jsonConfig.contains("pass"):
# raise InvalidConfigException.newException(
# fmt"This check ({this.id()}) requires the config property 'pass' (boolean) to be set")
TestingCheck(config: jsonConfig)

proc createGenerator*(): CheckGenerator =
Expand Down
2 changes: 1 addition & 1 deletion src/checks/unwanted_files_exist_not.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ method run*(this: UnwantedFilesExistNotCheck, state: var State): CheckResult =
method id*(this: UnwantedFilesExistNotCheckGenerator): string =
return ID

method generate*(this: UnwantedFilesExistNotCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: UnwantedFilesExistNotCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
UnwantedFilesExistNotCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/uses_dir_std.nim
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ please report to the developers of this tool here: <{OSH_TOOL_ISSUES_URL}>"""))
method id*(this: UsesDirStdCheckGenerator): string =
return ID

method generate*(this: UsesDirStdCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: UsesDirStdCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
UsesDirStdCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/vcs_public.nim
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ error message:
method id*(this: VcsPublicCheckGenerator): string =
return ID

method generate*(this: VcsPublicCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: VcsPublicCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
VcsPublicCheck()

Expand Down
2 changes: 1 addition & 1 deletion src/checks/vcs_used.nim
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ for example [git](https://git-scm.com/).""")
method id*(this: VcsUsedCheckGenerator): string =
return ID

method generate*(this: VcsUsedCheckGenerator, config: CheckConfig = newCheckConfig(ID)): Check =
method generate*(this: VcsUsedCheckGenerator, config: CheckConfig = this.defaultConfig()): Check =
this.ensureNonConfig(config)
VcsUsedCheck()

Expand Down
12 changes: 4 additions & 8 deletions src/checks_registry.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ method sort*(this: var ChecksRegistry) {.base.} =
method registerGenerators*(this: var ChecksRegistry) {.base.} =
registerAll("checks")

method getCheck*(this: var ChecksRegistry, config: CheckConfig = newCheckConfig("non-ID")): check.Check {.base.} =
method getCheck*(this: var ChecksRegistry, config: CheckConfig): check.Check {.base.} =
let id = config.id
let generator = this.index[id]
if this.checks.contains(id):
Expand All @@ -79,13 +79,9 @@ method getCheck*(this: var ChecksRegistry, config: CheckConfig = newCheckConfig(
this.checks[id] = check
return check

method getChecks*(this: var ChecksRegistry, config: Option[OrderedTable[string, CheckConfig]] = none[OrderedTable[string, CheckConfig]]()): OrderedTable[string, check.Check] {.base.} =
if config.isSome():
for primaryId, checkConfig in config.get():
discard this.getCheck(checkConfig)
else:
for primaryId, checkGenerator in this.index:
discard this.getCheck()
method getChecks*(this: var ChecksRegistry, config: OrderedTable[string, CheckConfig]): OrderedTable[string, check.Check] {.base.} =
for primaryId, checkConfig in config:
discard this.getCheck(checkConfig)
this.sort()
return this.checks

Expand Down

0 comments on commit 9a00be1

Please sign in to comment.