MCPcopy Index your code
hub / github.com/cheat/cheat / TestLocalCheatpathIntegration

Function TestLocalCheatpathIntegration

test/integration/cheatpath_integration_test.go:28–246  ·  view source on GitHub ↗

TestLocalCheatpathIntegration exercises the recursive .cheat directory discovery end-to-end: it builds the real cheat binary, sets up filesystem layouts, and verifies behaviour from the user's perspective.

(t *testing.T)

Source from the content-addressed store, hash-verified

26// discovery end-to-end: it builds the real cheat binary, sets up filesystem
27// layouts, and verifies behaviour from the user's perspective.
28func TestLocalCheatpathIntegration(t *testing.T) {
29 if runtime.GOOS == "windows" {
30 t.Skip("integration test uses Unix-specific env vars")
31 }
32
33 // Build the cheat binary once for all sub-tests.
34 binPath := filepath.Join(t.TempDir(), "cheat_test")
35 build := exec.Command("go", "build", "-o", binPath, "./cmd/cheat")
36 build.Dir = repoRoot(t)
37 if output, err := build.CombinedOutput(); err != nil {
38 t.Fatalf("failed to build cheat: %v\nOutput: %s", err, output)
39 }
40
41 // cheatEnv returns a minimal environment for the cheat binary.
42 cheatEnv := func(confPath, home string) []string {
43 return []string{
44 "CHEAT_CONFIG_PATH=" + confPath,
45 "HOME=" + home,
46 "PATH=" + os.Getenv("PATH"),
47 "EDITOR=vi",
48 }
49 }
50
51 // writeConfig writes a minimal valid config file referencing sheetsDir.
52 writeConfig := func(t *testing.T, dir, sheetsDir string) string {
53 t.Helper()
54 conf := fmt.Sprintf("---\neditor: vi\ncolorize: false\ncheatpaths:\n - name: base\n path: %s\n readonly: true\n", sheetsDir)
55 confPath := filepath.Join(dir, "conf.yml")
56 if err := os.WriteFile(confPath, []byte(conf), 0644); err != nil {
57 t.Fatalf("failed to write config: %v", err)
58 }
59 return confPath
60 }
61
62 t.Run("parent .cheat is discovered from subdirectory", func(t *testing.T) {
63 root := t.TempDir()
64
65 // Configured cheatpath (empty but must exist for validation)
66 sheetsDir := filepath.Join(root, "sheets")
67 os.MkdirAll(sheetsDir, 0755)
68
69 // .cheat at root with a cheatsheet
70 dotCheat := filepath.Join(root, ".cheat")
71 os.Mkdir(dotCheat, 0755)
72 os.WriteFile(
73 filepath.Join(dotCheat, "localsheet"),
74 []byte("---\nsyntax: bash\n---\necho hello from local\n"),
75 0644,
76 )
77
78 confPath := writeConfig(t, root, sheetsDir)
79
80 // Work from a subdirectory
81 workDir := filepath.Join(root, "src", "pkg")
82 os.MkdirAll(workDir, 0755)
83 env := cheatEnv(confPath, root)
84
85 // --directories should list "cwd" cheatpath

Callers

nothing calls this directly

Calls 2

repoRootFunction · 0.85
hasCwdCheatpathFunction · 0.85

Tested by

no test coverage detected