GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cache-cli/coverage_test.go at main · ubgo/cache-cli Expand file tree/ Copy pathcoverage_test.goMore file actions 290 lines (264 loc) · 8.61 KB / Copy pathcoverage_test.goFile metadata and controls290 lines (264 loc) · 8.61 KB 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 // coverage_test.go — exhaustive run() path coverage: every subcommand, every // exit code (0/1/2), -json variants, namespace isolation, flag-parse error, // and backend-unreachable error. Driven through run(...) with bytes buffers // like main_test.go's exec helper; asserts exit code + stdout/stderr. package main import ( "strings" "testing" ) // badAddr points the redis client at a closed/bogus address so backend calls // fail fast (exit 1). 127.0.0.1:1 is reliably refused. const badAddr = "127.0.0.1:1" func TestGetHitJSON(t *testing.T) { addr := mini(t) if code, _, _ := exec(t, addr, "set", "k", "hello"); code != 0 { t.Fatal("set failed") } code, out, _ := exec(t, addr, "-json", "get", "k") if code != 0 { t.Fatalf("get -json exit %d", code) } if !strings.Contains(out, `"key":"k"`) || !strings.Contains(out, `"value":"hello"`) { t.Fatalf("get -json output wrong: %q", out) } } func TestGetMissExit1(t *testing.T) { addr := mini(t) code, out, errb := exec(t, addr, "get", "absent") if code != 1 { t.Fatalf("get miss should exit 1, got %d", code) } if out != "" { t.Fatalf("miss should produce no stdout, got %q", out) } if !strings.Contains(errb, "not found") { t.Fatalf("expected 'not found' on stderr, got %q", errb) } } func TestGetBackendErrorExit1(t *testing.T) { code, _, errb := exec(t, badAddr, "get", "k") if code != 1 { t.Fatalf("get on unreachable backend should exit 1, got %d", code) } if !strings.Contains(errb, "error:") { t.Fatalf("expected 'error:' on stderr, got %q", errb) } } func TestGetWrongArgCountExit2(t *testing.T) { addr := mini(t) code, _, errb := exec(t, addr, "get") if code != 2 { t.Fatalf("get with no key should exit 2, got %d", code) } if !strings.Contains(errb, "need exactly one") { t.Fatalf("expected usage message, got %q", errb) } if code, _, _ := exec(t, addr, "get", "a", "b"); code != 2 { t.Fatalf("get with two keys should exit 2, got %d", code) } } func TestSetWithTTL(t *testing.T) { addr := mini(t) // Go's flag package stops at the first non-flag arg, so -ttl must precede // the subcommand (the README's trailing-flag example is non-functional). code, out, _ := exec(t, addr, "-ttl", "5m", "set", "k", "v") if code != 0 { t.Fatalf("set -ttl exit %d", code) } if strings.TrimSpace(out) != "OK" { t.Fatalf("expected OK, got %q", out) } } func TestSetJSON(t *testing.T) { addr := mini(t) code, out, _ := exec(t, addr, "-json", "-ttl", "1s", "set", "k", "v") if code != 0 { t.Fatalf("set -json exit %d", code) } if !strings.Contains(out, `"key":"k"`) || !strings.Contains(out, `"ttl":"1s"`) { t.Fatalf("set -json output wrong: %q", out) } } func TestSetWrongArgCountExit2(t *testing.T) { addr := mini(t) if code, _, _ := exec(t, addr, "set", "k"); code != 2 { t.Fatalf("set with one arg should exit 2, got %d", code) } if code, _, _ := exec(t, addr, "set"); code != 2 { t.Fatalf("set with no args should exit 2, got %d", code) } } func TestSetBackendErrorExit1(t *testing.T) { code, _, errb := exec(t, badAddr, "set", "k", "v") if code != 1 { t.Fatalf("set on unreachable backend should exit 1, got %d", code) } if !strings.Contains(errb, "error:") { t.Fatalf("expected 'error:' on stderr, got %q", errb) } } func TestDelOKAndJSON(t *testing.T) { addr := mini(t) _, _, _ = exec(t, addr, "set", "k", "v") code, out, _ := exec(t, addr, "del", "k") if code != 0 || strings.TrimSpace(out) != "OK" { t.Fatalf("del got code=%d out=%q", code, out) } // Idempotent: deleting an absent key still exits 0. if code, _, _ := exec(t, addr, "del", "k"); code != 0 { t.Fatalf("del absent should exit 0, got %d", code) } code, out, _ = exec(t, addr, "-json", "del", "k") if code != 0 || !strings.Contains(out, `"deleted":"k"`) { t.Fatalf("del -json got code=%d out=%q", code, out) } } func TestDelWrongArgCountExit2(t *testing.T) { addr := mini(t) if code, _, _ := exec(t, addr, "del"); code != 2 { t.Fatalf("del with no key should exit 2, got %d", code) } if code, _, _ := exec(t, addr, "del", "a", "b"); code != 2 { t.Fatalf("del with two keys should exit 2, got %d", code) } } func TestDelBackendErrorExit1(t *testing.T) { code, _, errb := exec(t, badAddr, "del", "k") if code != 1 { t.Fatalf("del on unreachable backend should exit 1, got %d", code) } if !strings.Contains(errb, "error:") { t.Fatalf("expected 'error:' on stderr, got %q", errb) } } func TestStatsPlainAndJSON(t *testing.T) { addr := mini(t) code, out, _ := exec(t, addr, "stats") if code != 0 { t.Fatalf("stats exit %d", code) } if !strings.Contains(out, "entries=") || !strings.Contains(out, "hit_ratio=") { t.Fatalf("stats plain output wrong: %q", out) } code, out, _ = exec(t, addr, "-json", "stats") if code != 0 { t.Fatalf("stats -json exit %d", code) } if !strings.HasPrefix(strings.TrimSpace(out), "{") { t.Fatalf("stats -json should be a JSON object: %q", out) } } func TestKeysPlainAndEmptyPrefix(t *testing.T) { addr := mini(t) _, _, _ = exec(t, addr, "set", "a:1", "x") _, _, _ = exec(t, addr, "set", "b:1", "y") code, out, _ := exec(t, addr, "keys", "a:") if code != 0 { t.Fatalf("keys exit %d", code) } if !strings.Contains(out, "a:1") || strings.Contains(out, "b:1") { t.Fatalf("keys plain prefix scan wrong: %q", out) } // Empty prefix (no arg) iterates everything. code, out, _ = exec(t, addr, "keys") if code != 0 { t.Fatalf("keys (empty prefix) exit %d", code) } if !strings.Contains(out, "a:1") || !strings.Contains(out, "b:1") { t.Fatalf("empty-prefix keys should list all: %q", out) } } func TestKeysJSONEmpty(t *testing.T) { addr := mini(t) code, out, _ := exec(t, addr, "-json", "keys", "nomatch:") if code != 0 { t.Fatalf("keys -json exit %d", code) } // No matches → json.Marshal(nil slice) yields "null". if strings.TrimSpace(out) != "null" { t.Fatalf("empty keys -json should be null, got %q", out) } } func TestKeysBackendErrorExit1(t *testing.T) { code, _, errb := exec(t, badAddr, "keys", "x:") if code != 1 { t.Fatalf("keys on unreachable backend should exit 1, got %d", code) } if !strings.Contains(errb, "error:") { t.Fatalf("expected 'error:' on stderr, got %q", errb) } } func TestHelpExit0(t *testing.T) { addr := mini(t) // Bare "help" reaches the switch and prints usage to stdout, exit 0. code, out, _ := exec(t, addr, "help") if code != 0 { t.Fatalf("help should exit 0, got %d", code) } if !strings.Contains(out, "USAGE:") { t.Fatalf("help should print usage to stdout, got %q", out) } } func TestDashHFlagInterceptedExit2(t *testing.T) { addr := mini(t) // -h and --help are consumed by flag.Parse (ErrHelp) before reaching the // switch, so they exit 2 via the flag-parse-error path. The "-h"/"--help" // switch labels are only reachable as a positional after another arg and // are effectively defensive (justified-uncovered). for _, h := range []string{"-h", "--help"} { if code, _, _ := exec(t, addr, h); code != 2 { t.Fatalf("%q should exit 2 (flag-intercepted), got %d", h, code) } } } func TestNoCommandExit2(t *testing.T) { addr := mini(t) code, _, errb := exec(t, addr) if code != 2 { t.Fatalf("no command should exit 2, got %d", code) } if !strings.Contains(errb, "USAGE:") { t.Fatalf("no command should print usage to stderr, got %q", errb) } } func TestUnknownCommandPrintsUsageExit2(t *testing.T) { addr := mini(t) code, _, errb := exec(t, addr, "frobnicate") if code != 2 { t.Fatalf("unknown command should exit 2, got %d", code) } if !strings.Contains(errb, "unknown command") || !strings.Contains(errb, "USAGE:") { t.Fatalf("unknown command stderr wrong: %q", errb) } } func TestFlagParseErrorExit2(t *testing.T) { addr := mini(t) // -nope is not a defined flag → flag.Parse fails → exit 2. code, _, _ := exec(t, addr, "-nope", "stats") if code != 2 { t.Fatalf("unknown flag should exit 2, got %d", code) } // Bad duration value for -ttl also fails flag parsing. if code, _, _ := exec(t, addr, "-ttl", "notaduration", "set", "k", "v"); code != 2 { t.Fatalf("bad -ttl should exit 2, got %d", code) } } func TestNamespaceIsolationJSON(t *testing.T) { addr := mini(t) if code, _, _ := exec(t, addr, "-ns", "svc:a", "set", "k", "v1"); code != 0 { t.Fatal("namespaced set a failed") } if code, _, _ := exec(t, addr, "-ns", "svc:b", "set", "k", "v2"); code != 0 { t.Fatal("namespaced set b failed") } code, out, _ := exec(t, addr, "-ns", "svc:a", "-json", "get", "k") if code != 0 || !strings.Contains(out, `"value":"v1"`) { t.Fatalf("ns svc:a get wrong: code=%d out=%q", code, out) } // keys under one namespace must not see the other's key. code, _, _ = exec(t, addr, "-ns", "svc:a", "keys", "") if code != 0 { t.Fatalf("ns keys exit %d", code) } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |