|
| 1 | +/* |
| 2 | +Copyright The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package validators |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "runtime" |
| 24 | + "testing" |
| 25 | +) |
| 26 | + |
| 27 | +// MarkdownOutput is an implementation of OutputSink that writes output in Markdown format to a file. |
| 28 | +type MarkdownOutput struct { |
| 29 | + f *os.File |
| 30 | + t *testing.T |
| 31 | +} |
| 32 | + |
| 33 | +func (o *MarkdownOutput) WriteText(text string) { |
| 34 | + o.printf("%s", text) |
| 35 | +} |
| 36 | + |
| 37 | +func (o *MarkdownOutput) Success(text string) { |
| 38 | + o.printf("✓ %s\n", text) |
| 39 | +} |
| 40 | + |
| 41 | +func (o *MarkdownOutput) Close() error { |
| 42 | + return o.f.Close() |
| 43 | +} |
| 44 | + |
| 45 | +func (o *MarkdownOutput) OnShellExec(command string, results *CommandResult) { |
| 46 | + o.printf("```bash\n> %s\n", command) |
| 47 | + o.printf("%s", results.Stdout()) |
| 48 | + o.printf("%s", results.Stderr()) |
| 49 | + |
| 50 | + if results.Err() != nil { |
| 51 | + o.printf("Error:\n```\n%v\n```\n", results.Err()) |
| 52 | + } |
| 53 | + |
| 54 | + o.printf("```\n") |
| 55 | +} |
| 56 | + |
| 57 | +func (o *MarkdownOutput) printf(format string, args ...interface{}) { |
| 58 | + s := fmt.Sprintf(format, args...) |
| 59 | + _, err := fmt.Fprintln(o.f, s) |
| 60 | + if err != nil { |
| 61 | + o.t.Fatalf("failed to write to markdown file: %v", err) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func createMarkdownOutput(t *testing.T) OutputSink { |
| 66 | + // Get file path and other info from the current caller frame (depth 0) |
| 67 | + _, testFilename, _, ok := runtime.Caller(2) |
| 68 | + if !ok { |
| 69 | + t.Fatal("Could not get test caller") |
| 70 | + } |
| 71 | + _, baseFilename, _, ok := runtime.Caller(1) |
| 72 | + if !ok { |
| 73 | + t.Fatal("Could not get test caller") |
| 74 | + } |
| 75 | + |
| 76 | + baseDir := filepath.Dir(baseFilename) |
| 77 | + |
| 78 | + testRelativePath, err := filepath.Rel(baseDir, testFilename) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("failed to get relative path: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + testRelativeDir := filepath.Dir(testRelativePath) |
| 84 | + |
| 85 | + artifactsDir := os.Getenv("ARTIFACTS") |
| 86 | + if artifactsDir == "" { |
| 87 | + artifactsDir = "_artifacts" |
| 88 | + } |
| 89 | + outputBase := filepath.Join(artifactsDir, "ai-conformance") |
| 90 | + |
| 91 | + outputPath := filepath.Join(outputBase, testRelativeDir, t.Name()+".md") |
| 92 | + |
| 93 | + if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil { |
| 94 | + t.Fatalf("failed to create output directory %v: %v", filepath.Dir(outputPath), err) |
| 95 | + } |
| 96 | + |
| 97 | + outputFile, err := os.Create(outputPath) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("failed to create markdown file: %v", err) |
| 100 | + } |
| 101 | + output := &MarkdownOutput{f: outputFile, t: t} |
| 102 | + return output |
| 103 | +} |
0 commit comments