-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdproof.go
More file actions
245 lines (203 loc) · 6.96 KB
/
mdproof.go
File metadata and controls
245 lines (203 loc) · 6.96 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Package mdproof provides a facade over the internal packages for parsing,
// executing, and reporting on Markdown runbooks.
package mdproof
import (
"io"
"time"
"github.com/runkids/mdproof/internal/assertion"
"github.com/runkids/mdproof/internal/config"
"github.com/runkids/mdproof/internal/core"
"github.com/runkids/mdproof/internal/coverage"
"github.com/runkids/mdproof/internal/executor"
"github.com/runkids/mdproof/internal/parser"
"github.com/runkids/mdproof/internal/report"
"github.com/runkids/mdproof/internal/runner"
)
// Type aliases — re-export all public types from internal packages.
type (
Step = core.Step
Expectation = core.Expectation
StepResult = core.StepResult
AssertionResult = core.AssertionResult
Report = core.Report
Summary = core.Summary
Meta = core.Meta
Runbook = core.Runbook
SubCommandResult = core.SubCommandResult
HookExecResult = core.HookExecResult
DebugEnvironment = core.DebugEnvironment
StepDebug = core.StepDebug
SourcePos = core.SourcePos
SourceRange = core.SourceRange
StepSource = core.StepSource
Config = config.Config
SandboxConfig = config.SandboxConfig
RunOptions = runner.RunOptions
HookResult = runner.HookResult
)
// Status constants
const (
StatusPassed = core.StatusPassed
StatusFailed = core.StatusFailed
StatusSkipped = core.StatusSkipped
)
// Executor mode constants
const (
ExecutorAuto = core.ExecutorAuto
ExecutorManual = core.ExecutorManual
)
// Assertion type constants
const (
AssertSubstring = core.AssertSubstring
AssertExitCode = core.AssertExitCode
AssertRegex = core.AssertRegex
AssertJQ = core.AssertJQ
AssertSnapshot = core.AssertSnapshot
)
// Default timeouts.
const (
DefaultStepTimeout = core.DefaultStepTimeout
DefaultSessionTimeout = core.DefaultSessionTimeout
)
// ConfigFileName is the conventional name for directory-level runbook config.
const ConfigFileName = config.ConfigFileName
// Isolation mode constants.
const (
IsolationShared = config.IsolationShared
IsolationPerRunbook = config.IsolationPerRunbook
)
// ValidIsolation reports whether s is a recognised isolation mode (or empty).
func ValidIsolation(s string) bool {
return config.ValidIsolation(s)
}
// ErrNotInContainer is returned when execution is attempted outside a container.
var ErrNotInContainer = executor.ErrNotInContainer
// --- Parser ---
// ParseRunbook reads a Markdown runbook and extracts metadata and steps.
func ParseRunbook(r io.Reader) (*Runbook, error) {
return parser.ParseRunbook(r)
}
// ParseInline scans a Markdown file for inline test blocks.
func ParseInline(r io.Reader, filename string) (*Runbook, error) {
return parser.ParseInline(r, filename)
}
// Classify determines execution mode for a step.
func Classify(s Step) string {
return parser.Classify(s)
}
// ClassifyAll batch classifies all steps.
func ClassifyAll(steps []Step) []Step {
return parser.ClassifyAll(steps)
}
// --- Assertion ---
// RunAssertions checks all expected patterns against a step result.
func RunAssertions(result *StepResult, expected []Expectation) []AssertionResult {
return assertion.RunAssertions(result, expected)
}
// MatchAssertions checks each expected pattern against the command output.
func MatchAssertions(output string, expected []string) []AssertionResult {
return assertion.MatchAssertions(output, expected)
}
// AllPassed returns true when every assertion matched successfully.
func AllPassed(results []AssertionResult) bool {
return assertion.AllPassed(results)
}
// --- Executor ---
// IsContainerEnv returns true if we're running inside a Docker container
// or if the MDPROOF_ALLOW_EXECUTE env var is set.
func IsContainerEnv() bool {
return executor.IsContainerEnv()
}
// --- Config ---
// LoadConfig reads an mdproof.json from the given directory.
func LoadConfig(dir string) (Config, error) {
return config.Load(dir)
}
// MergeConfig applies CLI flag overrides on top of file-based config.
// strictExplicit indicates whether --strict was explicitly passed on the CLI.
func MergeConfig(
file Config,
cliBuild, cliSetup, cliTeardown, cliStepSetup, cliStepTeardown string,
cliTimeout time.Duration,
cliStrict bool,
strictExplicit bool,
cliIsolation string,
cliKeepFailedArtifacts bool,
keepFailedArtifactsExplicit bool,
cliPrintStepScript bool,
printStepScriptExplicit bool,
cliPrintStepEnv bool,
printStepEnvExplicit bool,
cliWorkdir string,
) Config {
return config.Merge(
file,
cliBuild, cliSetup, cliTeardown, cliStepSetup, cliStepTeardown,
cliTimeout,
cliStrict,
strictExplicit,
cliIsolation,
cliKeepFailedArtifacts,
keepFailedArtifactsExplicit,
cliPrintStepScript,
printStepScriptExplicit,
cliPrintStepEnv,
printStepEnvExplicit,
cliWorkdir,
)
}
// --- Report ---
// WriteJSONReport writes the report as indented JSON.
func WriteJSONReport(w io.Writer, r Report) error {
return report.WriteJSONReport(w, r)
}
// WriteJSONReports writes multiple reports as a JSON array.
func WriteJSONReports(w io.Writer, reports []Report) error {
return report.WriteJSONReports(w, reports)
}
// WriteJUnitReport writes the reports as JUnit XML.
func WriteJUnitReport(w io.Writer, reports []Report) error {
return report.WriteJUnitReport(w, reports)
}
// WriteGitHubReport writes GitHub Actions workflow commands for failed steps.
func WriteGitHubReport(w io.Writer, reports []Report) {
report.WriteGitHubReport(w, reports)
}
// WriteSingleReport prints a single runbook result in plain text.
func WriteSingleReport(w io.Writer, r Report, verbosity int) {
report.WriteSingleReport(w, r, verbosity)
}
// WritePlainSummary prints a multi-runbook batch summary.
func WritePlainSummary(w io.Writer, reports []Report, verbosity int) {
report.WritePlainSummary(w, reports, verbosity)
}
// --- Coverage ---
// CoverageEntry pairs a file name with its coverage result.
type CoverageEntry = report.CoverageEntry
// CoverageResult holds coverage analysis for a set of steps.
type CoverageResult = coverage.Result
// AnalyzeCoverage computes coverage metrics for a list of steps.
func AnalyzeCoverage(steps []Step) coverage.Result {
return coverage.Analyze(steps)
}
// WriteCoverageReport writes a plain-text coverage table.
func WriteCoverageReport(w io.Writer, entries []CoverageEntry) {
report.WriteCoverageReport(w, entries)
}
// CoverageTotalScore computes the aggregate score across all entries.
func CoverageTotalScore(entries []CoverageEntry) int {
return report.TotalScore(entries)
}
// --- Runner ---
// Run parses, classifies, executes, and reports a runbook.
func Run(r io.Reader, name string, opts RunOptions) (Report, error) {
return runner.Run(r, name, opts)
}
// RunBuildHook executes the build command as a simple shell process.
func RunBuildHook(command string) *HookResult {
return runner.RunBuildHook(command)
}
// ResolveFiles finds runbook/proof files from a path (file or directory).
func ResolveFiles(target string) ([]string, error) {
return runner.ResolveFiles(target)
}