diff options
| author | Jonathan Amsterdam <jba@google.com> | 2019-10-15 06:00:58 -0400 |
|---|---|---|
| committer | Julie Qiu <julie@golang.org> | 2020-03-27 16:46:45 -0400 |
| commit | e2f17ff2cdc073a57b62ea4dc927275afd013f8b (patch) | |
| tree | 17c69d77ca9f2dad9cd819752f5c8a52dad2427c /internal/source/source_test.go | |
| parent | 93c2ef87db810128607f13d217bdc81041c72c5b (diff) | |
| download | go-x-pkgsite-e2f17ff2cdc073a57b62ea4dc927275afd013f8b.tar.xz | |
internal/source,postgres: save source info to the DB
- Add a SourceInfo field to VersionInfo.
- Add functions to convert a source.Info to and from JSON.
- Modify postgres statements to read and write from the DB.
Keep the VersionInfo.RepositoryURL field around for now to avoid
messing with the frontend.
Updates b/132219298, b/138752200.
Change-Id: I6ebb3af3afa14fda92c2804ab2d0294742a3bf41
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/575156
CI-Result: Cloud Build <devtools-proctor-result-processor@system.gserviceaccount.com>
Reviewed-by: Julie Qiu <julieqiu@google.com>
Diffstat (limited to 'internal/source/source_test.go')
| -rw-r--r-- | internal/source/source_test.go | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/internal/source/source_test.go b/internal/source/source_test.go index 9cf46a7d..08931226 100644 --- a/internal/source/source_test.go +++ b/internal/source/source_test.go @@ -6,6 +6,7 @@ package source import ( "context" + "encoding/json" "flag" "fmt" "io/ioutil" @@ -251,7 +252,7 @@ func TestModuleInfo(t *testing.T) { t.Fatal(err) } - check("repo", info.RepoURL, test.wantRepo) + check("repo", info.repoURL, test.wantRepo) check("module", info.ModuleURL(), test.wantModule) check("file", info.FileURL(test.file), test.wantFile) check("line", info.LineURL(test.file, 1), test.wantLine) @@ -331,7 +332,7 @@ func TestModuleImportDynamic(t *testing.T) { { "alice.org/pkg", &Info{ - RepoURL: "https://github.com/alice/pkg", + repoURL: "https://github.com/alice/pkg", moduleDir: "", commit: "v1.2.3", templates: githubURLTemplates, @@ -340,7 +341,7 @@ func TestModuleImportDynamic(t *testing.T) { { "alice.org/pkg/sub", &Info{ - RepoURL: "https://github.com/alice/pkg", + repoURL: "https://github.com/alice/pkg", moduleDir: "sub", commit: "sub/v1.2.3", templates: githubURLTemplates, @@ -349,7 +350,7 @@ func TestModuleImportDynamic(t *testing.T) { { "alice.org/pkg/http", &Info{ - RepoURL: "https://github.com/alice/pkg", + repoURL: "https://github.com/alice/pkg", moduleDir: "http", commit: "http/v1.2.3", templates: githubURLTemplates, @@ -359,7 +360,7 @@ func TestModuleImportDynamic(t *testing.T) { "alice.org/pkg/source", // Has a go-source tag, but we can't use the templates. &Info{ - RepoURL: "http://alice.org/pkg", + repoURL: "http://alice.org/pkg", moduleDir: "source", commit: "source/v1.2.3", // empty templates @@ -370,7 +371,7 @@ func TestModuleImportDynamic(t *testing.T) { "alice.org/pkg/ignore", // Stop at the first go-source. &Info{ - RepoURL: "http://alice.org/pkg", + repoURL: "http://alice.org/pkg", moduleDir: "ignore", commit: "ignore/v1.2.3", // empty templates @@ -383,7 +384,7 @@ func TestModuleImportDynamic(t *testing.T) { &Info{ // The go-import tag's repo root ends in ".git", but according to the spec // there should not be a .vcs suffix, so we include the ".git" in the repo URL. - RepoURL: "https://vcs.net/bob/pkg.git", + repoURL: "https://vcs.net/bob/pkg.git", moduleDir: "", commit: "v1.2.3", // empty templates @@ -392,7 +393,7 @@ func TestModuleImportDynamic(t *testing.T) { { "bob.com/pkg/sub", &Info{ - RepoURL: "https://vcs.net/bob/pkg.git", + repoURL: "https://vcs.net/bob/pkg.git", moduleDir: "sub", commit: "sub/v1.2.3", // empty templates @@ -403,7 +404,7 @@ func TestModuleImportDynamic(t *testing.T) { // The go-source tag has a template that is handled incorrectly by godoc; but we // ignore the templates. &Info{ - RepoURL: "https://github.com/azul3d/examples", + repoURL: "https://github.com/azul3d/examples", moduleDir: "abs", commit: "abs/v1.2.3", templates: githubURLTemplates, @@ -413,7 +414,7 @@ func TestModuleImportDynamic(t *testing.T) { "myitcv.io/blah2", // Ignore the "mod" vcs type. &Info{ - RepoURL: "https://github.com/myitcv/x", + repoURL: "https://github.com/myitcv/x", moduleDir: "", commit: "v1.2.3", templates: githubURLTemplates, @@ -422,7 +423,7 @@ func TestModuleImportDynamic(t *testing.T) { { "alice.org/pkg/default", &Info{ - RepoURL: "https://github.com/alice/pkg", + repoURL: "https://github.com/alice/pkg", moduleDir: "default", commit: "default/v1.2.3", templates: githubURLTemplates, @@ -530,10 +531,10 @@ func TestAdjustVersionedModuleDirectory(t *testing.T) { } { t.Run(test.repo+","+test.moduleDir+","+test.commit, func(t *testing.T) { info := &Info{ - RepoURL: "http://x.com/" + test.repo, + repoURL: "http://x.com/" + test.repo, moduleDir: test.moduleDir, commit: test.commit, - templates: urlTemplates{file: "{repo}/{commit}/{file}"}, + templates: urlTemplates{File: "{repo}/{commit}/{file}"}, } adjustVersionedModuleDirectory(ctx, client, info) got := info.moduleDir @@ -674,3 +675,48 @@ var testWeb = map[string]string{ `<meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master">` + `</head>`, } + +func TestJSON(t *testing.T) { + for _, test := range []struct { + in *Info + want string + }{ + { + nil, + `null`, + }, + { + &Info{repoURL: "r", moduleDir: "m", commit: "c"}, + `{"RepoURL":"r","ModuleDir":"m","Commit":"c"}`, + }, + { + &Info{repoURL: "r", moduleDir: "m", commit: "c", templates: githubURLTemplates}, + `{"RepoURL":"r","ModuleDir":"m","Commit":"c","Kind":"github"}`, + }, + { + &Info{repoURL: "r", moduleDir: "m", commit: "c", templates: urlTemplates{File: "f"}}, + `{"RepoURL":"r","ModuleDir":"m","Commit":"c","Templates":{"Directory":"","File":"f","Line":"","Raw":""}}`, + }, + } { + bytes, err := json.Marshal(&test.in) + if err != nil { + t.Fatal(err) + } + got := string(bytes) + if got != test.want { + t.Errorf("%#v:\ngot %s\nwant %s", test.in, got, test.want) + continue + } + var out Info + if err := json.Unmarshal(bytes, &out); err != nil { + t.Fatal(err) + } + var want Info + if test.in != nil { + want = *test.in + } + if out != want { + t.Errorf("got %#v\nwant %#v", out, want) + } + } +} |
