fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

1package main 2 3import ( 4 "bytes" 5 "context" 6 "errors" 7 "log" 8 "os" 9 "os/exec" 10 "reflect" 11 "strings" 12 "testing" 13 "time" 14) 15 16var ( 17 cmdTimeout = 100 * time.Millisecond 18) 19 20func captureOutput(f func()) string { 21 var buf bytes.Buffer 22 log.SetOutput(&buf) 23 defer func() { log.SetOutput(os.Stderr) }() 24 f() 25 return buf.String() 26} 27 28func TestLoggedRun(t *testing.T) { 29 ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout) 30 defer cancel() 31 32 cmd := exec.CommandContext(ctx, "echo", "-n", "1") 33 34 stdout := captureOutput(func() { 35 loggedRun(cmd) 36 }) 37 38 if !strings.Contains(stdout, "run [echo -n 1]") { 39 t.Errorf("loggedRun output is incorrect: %v", stdout) 40 } 41} 42 43func TestLoggedRunFailure(t *testing.T) { 44 ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout) 45 defer cancel() 46 47 cmd := exec.CommandContext(ctx, "false") 48 49 stdout := captureOutput(func() { 50 loggedRun(cmd) 51 }) 52 53 if !strings.Contains(stdout, "failed: exit status 1") { 54 t.Errorf("loggedRun output is incorrect: %v", stdout) 55 } 56} 57 58func TestInitMetrics(t *testing.T) { 59 server := indexServer{} 60 61 server.initMetrics() 62 63 if server.promRegistry == nil { 64 t.Errorf("promRegistry shouldn't be nil") 65 } 66 67 if server.metricsRequestsTotal == nil { 68 t.Errorf("metricsRequestsTotal shouldn't be nil") 69 } 70} 71 72func TestIndexRepository(t *testing.T) { 73 var cmdHistory [][]string 74 75 executeCmd = func(ctx context.Context, name string, arg ...string) (err error) { 76 currentCmd := append([]string{name}, arg...) 77 cmdHistory = append(cmdHistory, currentCmd) 78 79 return 80 } 81 82 opts := Options{ 83 indexTimeout: cmdTimeout, 84 repoDir: "/repo_dir", 85 indexDir: "/index_dir", 86 } 87 88 req := indexRequest{ 89 CloneURL: "https://example.com/repository.git", 90 RepoID: 100, 91 } 92 93 _, err := indexRepository(opts, req) 94 95 if err != nil { 96 t.Fatal(err) 97 } 98 99 expectedHistory := [][]string{ 100 {"zoekt-git-clone", "-dest", "/repo_dir", "-name", "100", "-repoid", "100", "https://example.com/repository.git"}, 101 {"git", "-C", "/repo_dir/100.git", "fetch"}, 102 {"zoekt-git-index", "-index", "/index_dir", "/repo_dir/100.git"}, 103 } 104 105 if !reflect.DeepEqual(cmdHistory, expectedHistory) { 106 t.Errorf("cmdHistory output is incorrect: %v, expected output: %v", cmdHistory, expectedHistory) 107 } 108} 109 110func TestIndexRepositoryWhenErr(t *testing.T) { 111 var cmdHistory [][]string 112 113 executeCmd = func(ctx context.Context, name string, arg ...string) (err error) { 114 currentCmd := append([]string{name}, arg...) 115 cmdHistory = append(cmdHistory, currentCmd) 116 117 if len(cmdHistory) > 1 { 118 return errors.New("command failed") 119 } 120 121 return 122 } 123 124 opts := Options{ 125 indexTimeout: cmdTimeout, 126 repoDir: "/repo_dir", 127 indexDir: "/index_dir", 128 } 129 130 req := indexRequest{ 131 CloneURL: "https://example.com/repository.git", 132 RepoID: 100, 133 } 134 135 _, err := indexRepository(opts, req) 136 137 if err == nil { 138 t.Errorf("Error is empty, when it should be present") 139 } 140 141 expectedHistory := [][]string{ 142 {"zoekt-git-clone", "-dest", "/repo_dir", "-name", "100", "-repoid", "100", "https://example.com/repository.git"}, 143 {"git", "-C", "/repo_dir/100.git", "fetch"}, 144 } 145 146 if !reflect.DeepEqual(cmdHistory, expectedHistory) { 147 t.Errorf("cmdHistory output is incorrect: %v, expected output: %v", cmdHistory, expectedHistory) 148 } 149}