forked from corbym/gogiven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
61 lines (54 loc) · 2.12 KB
/
generator.go
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
package gogiven
import (
"fmt"
"github.com/corbym/gogiven/generator"
"github.com/corbym/htmlspec"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// Generator is a global variable that holds the GoGivensOutputGenerator.
// You can replace the generator with your own if you match the interface here
// and set Generator = new(myFooGenerator) in a method (usually TestMain or init).
// Don't forget to add the call to the generator function in a "func TestMain(testing.M)" method
// in your test package.
// One file per test file will be generated containing output.
var Generator generator.GoGivensOutputGenerator = htmlspec.NewTestOutputGenerator()
func transformFileNameToHeader(fileName string) (header string) {
return strings.Title(strings.Replace(strings.TrimSuffix(filepath.Base(fileName), ".go"), "_", " ", -1))
}
// GenerateTestOutput generates the test output. Call this method from TestMain.
func GenerateTestOutput() {
for _, key := range globalTestContextMap.Keys() {
value, _ := globalTestContextMap.Load(key)
currentTestContext := value.(*TestContext)
tests := currentTestContext.SomeTests()
pageData := &generator.PageData{
Title: transformFileNameToHeader(currentTestContext.FileName()),
SomeMap: tests.AsMapOfSome(),
}
output := Generator.Generate(pageData)
extension := Generator.FileExtension()
outputFileName := fmt.Sprintf("%s%c%s", outputDirectory(),
os.PathSeparator,
strings.Replace(filepath.Base(currentTestContext.fileName), ".go", extension, 1))
err := ioutil.WriteFile(outputFileName, []byte(output), 0644)
if err != nil {
panic("error generating gogiven output:" + err.Error())
}
fmt.Printf("\ngenerated test output: file://%s\n", strings.Replace(outputFileName, "\\", "/", -1))
}
}
func outputDirectory() string {
outputDir := os.Getenv("GOGIVENS_OUTPUT_DIR")
if outputDir == "" {
os.Stdout.WriteString("env var GOGIVENS_OUTPUT_DIR was not found, using TempDir " + os.TempDir())
outputDir = os.TempDir()
}
if _, err := os.Stat(outputDir); err == nil {
return outputDir
}
os.Stderr.WriteString("output dir not found:" + outputDir + ", defaulting to ./")
return "."
}