File: editorconfig_test.go

package info (click to toggle)
golang-mvdan-editorconfig 0.2.0%2Bgit20240816.eab549b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 388 kB
  • sloc: makefile: 9
file content (90 lines) | stat: -rw-r--r-- 1,889 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2019, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information

package editorconfig

import (
	"fmt"
	"os"
	"os/exec"
	"regexp"
	"sync"
	"testing"
)

func TestMain(m *testing.M) {
	if os.Getenv("EDITORCONFIG_CMD") != "" {
		cmd()
		os.Exit(0)
	}
	// call flag.Parse() here if TestMain uses flags
	os.Exit(m.Run())
}

func run(dir, command string, args ...string) (string, error) {
	cmd := exec.Command(command, args...)
	cmd.Dir = dir
	out, err := cmd.CombinedOutput()
	return string(out), err
}

func mustRun(t *testing.T, dir, command string, args ...string) {
	out, err := run(dir, command, args...)
	if err != nil {
		fmt.Println(out)
		t.Fatal(err)
	}
}

func TestViaCmake(t *testing.T) {
	os.Setenv("EDITORCONFIG_CMD", os.Args[0])
	mustRun(t, "core-test", "cmake", "..")

	// Run with a high number of parallel jobs, and with a reduced sleep
	// before exit when using -race, as we have a lot of test cases to run.
	os.Setenv("GORACE", "atexit_sleep_ms=10")
	out, err := run("core-test", "ctest", "-j8")
	if err != nil {
		rxFailed := regexp.MustCompile(` - ([a-zA-Z0-9_]+) \((.*)\)`)
		matches := rxFailed.FindAllStringSubmatch(out, -1)
		if len(matches) == 0 {
			// something went very wrong
			fmt.Println(out)
			t.Fatal(err)
		}

		for _, match := range matches {
			name, reason := match[1], match[2]
			t.Errorf("%s failed: %s", name, reason)
		}
	}
}

func TestConcurrentQuery(t *testing.T) {
	q := Query{}
	n := 100
	name := "_sample/subdir/code.go"

	many := func(fn func()) {
		var wg sync.WaitGroup
		wg.Add(n)
		for i := 0; i < n; i++ {
			go func() {
				fn()
				wg.Done()
			}()
		}
		wg.Wait()
	}

	many(func() {
		section, err := q.Find(name, nil)
		if err != nil {
			t.Error(err)
		}
		if exp, got := 4, len(section.Properties); exp != got {
			t.Errorf("wanted %d properties, got %d", exp, got)
		}
		_ = section.String()
	})
}