-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.nim
113 lines (97 loc) · 3.4 KB
/
testing.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# This file is part of osh-tool.
# <https://github.com/hoijui/osh-tool>
#
# SPDX-FileCopyrightText: 2023 Robin Vobruba <[email protected]>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
import options
import strformat
import std/json
import tables
import ../check
import ../check_config
import ../state
import ../util/fs
#const IDS = @[srcFileNameBase()]
const ID = srcFileNameBase()
const CONFIG_JSON_SCHEMA = """
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://raw.githubusercontent.com/hoijui/osh-tool/master/src/checks/testing.config.schema.json",
"title": "OSH-Tool - check: testing - configuration",
"description": "The JSON-Schema to validate the configuration for the osh-tool check 'testing', defined in testing.nim.",
"type": "object",
"properties": {
"pass": {
"description": "Whether this check should pass (true) or fail (false)",
"type": "boolean"
}
},
"required": [ "pass" ]
}
"""
type TestingCheck = ref object of Check
config: JsonNode
type TestingCheckGenerator = ref object of CheckGenerator
method name*(this: TestingCheck): string =
return "Tool internal testing"
method description*(this: TestingCheck): string =
return fmt"""This check is only for those testing development of checks for this tool. \
Do not use it in production!"""
method why*(this: TestingCheck): string =
return """Checking out this tests source code, \
one can learn how to write a check with a custom configuration."""
method sourcePath*(this: TestingCheck): string =
return fs.srcFileName()
method requirements*(this: TestingCheck): CheckReqs =
return { }
method getSignificanceFactors*(this: TestingCheck): CheckSignificance =
return CheckSignificance(
weight: 0.0,
openness: 0.0,
hardware: 0.0,
quality: 0.0,
machineReadability: 0.0,
)
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
method run*(this: TestingCheck, state: var State): CheckResult =
let config = state.config.checks[ID]
let pass = this.config["pass"].getBool()
return (if pass:
newCheckResult(config, CheckResultKind.Perfect)
else:
newCheckResult(
config,
CheckResultKind.Bad,
CheckIssueSeverity.High,
some("""This test was configured to fail.""")
)
)
method id*(this: TestingCheckGenerator): string =
return ID
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 =
TestingCheckGenerator()