this repo has no description
0
fork

Configure Feed

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

mod/modregistrytest: add metadata capability

We want the ability to predicate some behavior on the metadata uploaded
with a module, so add the capability to specify module metadata inside
mod/modregistrytest.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I934b4aaf004b0ac9e2ca6e03c37b3fc0d2cc581c
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1235499
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+62 -4
+15 -4
mod/modregistrytest/registry.go
··· 354 354 if err := m.writeZip(&zipContent); err != nil { 355 355 return err 356 356 } 357 - if err := client.PutModule(ctx, v, bytes.NewReader(zipContent.Bytes()), int64(zipContent.Len())); err != nil { 357 + if err := client.PutModuleWithMetadata(ctx, v, bytes.NewReader(zipContent.Bytes()), int64(zipContent.Len()), m.metadata); err != nil { 358 358 return err 359 359 } 360 360 pushed[v] = true ··· 448 448 if err != nil { 449 449 return err 450 450 } 451 + if rest == moduleMetaFile { 452 + var meta modregistry.Metadata 453 + if err := json.Unmarshal(data, &meta); err != nil { 454 + return fmt.Errorf("cannot parse %s in %s: %v", moduleMetaFile, modver, err) 455 + } 456 + content.metadata = &meta 457 + return nil 458 + } 451 459 content.files = append(content.files, txtar.File{ 452 460 Name: rest, 453 461 Data: data, ··· 469 477 } 470 478 471 479 type moduleContent struct { 472 - version module.Version 473 - files []txtar.File 474 - modFile *modfile.File 480 + version module.Version 481 + files []txtar.File 482 + modFile *modfile.File 483 + metadata *modregistry.Metadata 475 484 } 476 485 477 486 func (c *moduleContent) writeZip(w io.Writer) error { 478 487 return modzip.Create(w, c.version, c.files, txtarFileIO{}) 479 488 } 489 + 490 + const moduleMetaFile = "_module_meta.json" 480 491 481 492 func (c *moduleContent) init(versDir string) error { 482 493 found := false
+34
mod/modregistrytest/registry_test.go
··· 2 2 3 3 import ( 4 4 "context" 5 + "encoding/json" 5 6 "fmt" 6 7 "os" 7 8 "path" ··· 80 81 } 81 82 if string(gotData) != string(wantData) { 82 83 t.Errorf("unexpected GET response\ngot %q\nwant %q", gotData, wantData) 84 + } 85 + case "metadata": 86 + if len(args) < 3 { 87 + t.Fatalf("usage: metadata $version key=value...") 88 + } 89 + mv, err := module.ParseVersion(args[1]) 90 + if err != nil { 91 + t.Fatalf("invalid version %q in metadata", args[1]) 92 + } 93 + m, err := client.GetModule(ctx, mv) 94 + if err != nil { 95 + t.Fatal(err) 96 + } 97 + meta, err := m.Metadata() 98 + if err != nil { 99 + t.Fatal(err) 100 + } 101 + gotData, err := json.Marshal(meta) 102 + if err != nil { 103 + t.Fatal(err) 104 + } 105 + var gotMap map[string]string 106 + if err := json.Unmarshal(gotData, &gotMap); err != nil { 107 + t.Fatal(err) 108 + } 109 + for _, kv := range args[2:] { 110 + k, v, ok := strings.Cut(kv, "=") 111 + if !ok { 112 + t.Fatalf("invalid key=value %q", kv) 113 + } 114 + if got := gotMap[k]; got != v { 115 + t.Errorf("metadata %q: got %q, want %q", k, got, v) 116 + } 83 117 } 84 118 default: 85 119 t.Fatalf("unknown command %q", line)
+13
mod/modregistrytest/testdata/metadata.txtar
··· 1 + metadata foo.com/bar/hello@v0.2.3 org.cuelang.vcs-type=git org.cuelang.vcs-commit=abc123def456 org.cuelang.vcs-commit-time=2024-07-15T10:30:00Z 2 + 3 + -- foo.com_bar_hello_v0.2.3/_module_meta.json -- 4 + { 5 + "org.cuelang.vcs-type": "git", 6 + "org.cuelang.vcs-commit": "abc123def456", 7 + "org.cuelang.vcs-commit-time": "2024-07-15T10:30:00Z" 8 + } 9 + -- foo.com_bar_hello_v0.2.3/cue.mod/module.cue -- 10 + module: "foo.com/bar/hello@v0" 11 + language: version: "v0.8.0" 12 + -- foo.com_bar_hello_v0.2.3/x.cue -- 13 + package hello