summaryrefslogtreecommitdiffstats
path: root/pkg/integration/tests/test_list_generator.go
blob: 6951dad57f66f900554bbfb9e857286ee0b96ca4 (plain)
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
//go:build ignore

// This file is invoked with `go generate ./...` and it generates the test_list.go file
// The test_list.go file is a list of all the integration tests.
// It's annoying to have to manually add an entry in that file for each test you
// create, so this generator is here to make the process easier.

package main

import (
	"bytes"
	"fmt"
	"go/format"
	"io/fs"
	"os"
	"strings"

	"github.com/samber/lo"
)

func main() {
	println("Generating test_list.go...")

	code := generateCode()

	formattedCode, err := format.Source(code)
	if err != nil {
		panic(err)
	}
	if err := os.WriteFile("test_list.go", formattedCode, 0o644); err != nil {
		panic(err)
	}
}

func generateCode() []byte {
	// traverse parent directory to get all subling directories
	directories, err := os.ReadDir("../tests")
	if err != nil {
		panic(err)
	}

	directories = lo.Filter(directories, func(file fs.DirEntry, _ int) bool {
		// 'shared' is a special folder containing shared test code so we
		// ignore it here
		return file.IsDir() && file.Name() != "shared"
	})

	var buf bytes.Buffer
	fmt.Fprintf(&buf, "// THIS FILE IS AUTO-GENERATED. You can regenerate it by running `go generate ./...` at the root of the lazygit repo.\n\n")
	fmt.Fprintf(&buf, "package tests\n\n")
	fmt.Fprintf(&buf, "import (\n")
	fmt.Fprintf(&buf, "\t\"github.com/jesseduffield/lazygit/pkg/integration/components\"\n")
	for _, dir := range directories {
		fmt.Fprintf(&buf, "\t\"github.com/jesseduffield/lazygit/pkg/integration/tests/%s\"\n", dir.Name())
	}
	fmt.Fprintf(&buf, ")\n\n")
	fmt.Fprintf(&buf, "var tests = []*components.IntegrationTest{\n")
	for _, dir := range directories {
		appendDirTests(dir, &buf)
	}
	fmt.Fprintf(&buf, "}\n")

	return buf.Bytes()
}

func appendDirTests(dir fs.DirEntry, buf *bytes.Buffer) {
	files, err := os.ReadDir(fmt.Sprintf("../tests/%s", dir.Name()))
	if err != nil {
		panic(err)
	}

	for _, file := range files {
		if file.IsDir() || !strings.HasSuffix(file.Name(), ".go") {
			continue
		}

		testName := snakeToPascal(
			strings.TrimSuffix(file.Name(), ".go"),
		)

		fileContents, err := os.ReadFile(fmt.Sprintf("../tests/%s/%s", dir.Name(), file.Name()))
		if err != nil {
			panic(err)
		}

		fileContentsStr := string(fileContents)

		if !strings.Contains(fileContentsStr, "NewIntegrationTest(") {
			// the file does not define a test so it probably just contains shared test code
			continue
		}

		if !strings.Contains(fileContentsStr, fmt.Sprintf("var %s = NewIntegrationTest(NewIntegrationTestArgs{", testName)) {
			panic(fmt.Sprintf("expected test %s to be defined in file %s. Perhaps you misspelt it? The name of the test should be the name of the file but converted from snake_case to PascalCase", testName, file.Name()))
		}

		fmt.Fprintf(buf, "\t%s.%s,\n", dir.Name(), testName)
	}
}

// thanks ChatGPT
func snakeToPascal(s string) string {
	// Split the input string into words.
	words := strings.Split(s, "_")

	// Convert the first letter of each word to uppercase and concatenate them.
	var builder strings.Builder
	for _, w := range words {
		if len(w) > 0 {
			builder.WriteString(strings.ToUpper(w[:1]))
			builder.WriteString(w[1:])
		}
	}

	return builder.String()
}