File: nodot_command.go

package info (click to toggle)
golang-ginkgo 1.2.0%2Bgit20161006.acfa16a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,324 kB
  • ctags: 1,210
  • sloc: makefile: 12
file content (76 lines) | stat: -rw-r--r-- 1,966 bytes parent folder | download | duplicates (2)
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
package main

import (
	"bufio"
	"flag"
	"github.com/onsi/ginkgo/ginkgo/nodot"
	"io/ioutil"
	"os"
	"path/filepath"
	"regexp"
)

func BuildNodotCommand() *Command {
	return &Command{
		Name:         "nodot",
		FlagSet:      flag.NewFlagSet("bootstrap", flag.ExitOnError),
		UsageCommand: "ginkgo nodot",
		Usage: []string{
			"Update the nodot declarations in your test suite",
			"Any missing declarations (from, say, a recently added matcher) will be added to your bootstrap file.",
			"If you've renamed a declaration, that name will be honored and not overwritten.",
		},
		Command: updateNodot,
	}
}

func updateNodot(args []string, additionalArgs []string) {
	suiteFile, perm := findSuiteFile()

	data, err := ioutil.ReadFile(suiteFile)
	if err != nil {
		complainAndQuit("Failed to update nodot declarations: " + err.Error())
	}

	content, err := nodot.ApplyNoDot(data)
	if err != nil {
		complainAndQuit("Failed to update nodot declarations: " + err.Error())
	}
	ioutil.WriteFile(suiteFile, content, perm)

	goFmt(suiteFile)
}

func findSuiteFile() (string, os.FileMode) {
	workingDir, err := os.Getwd()
	if err != nil {
		complainAndQuit("Could not find suite file for nodot: " + err.Error())
	}

	files, err := ioutil.ReadDir(workingDir)
	if err != nil {
		complainAndQuit("Could not find suite file for nodot: " + err.Error())
	}

	re := regexp.MustCompile(`RunSpecs\(|RunSpecsWithDefaultAndCustomReporters\(|RunSpecsWithCustomReporters\(`)

	for _, file := range files {
		if file.IsDir() {
			continue
		}
		path := filepath.Join(workingDir, file.Name())
		f, err := os.Open(path)
		if err != nil {
			complainAndQuit("Could not find suite file for nodot: " + err.Error())
		}
		defer f.Close()

		if re.MatchReader(bufio.NewReader(f)) {
			return path, file.Mode()
		}
	}

	complainAndQuit("Could not find a suite file for nodot: you need a bootstrap file that call's Ginkgo's RunSpecs() command.\nTry running ginkgo bootstrap first.")

	return "", 0
}