Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions cmd/src/search_stream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package main
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A different way to test this would be to start a streaming server and return your mocked events, then you can just diff against the total output of src-cli search -stream. That should cover a lot more code paths and be a test that is less likely to change as you change internal details.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Let's remove autogold then. Doing both, spinning up a stream server and using autogold is an overkill.


import (
"bytes"
"testing"

"github.com/hexops/autogold"

"github.com/sourcegraph/src-cli/internal/streaming"
)

func TestRepoTemplate(t *testing.T) {
v, err := parseTemplate(streamingTemplate)
if err != nil {
t.Fatal(err)
}

repo := &streaming.EventRepoMatch{
Type: streaming.RepoMatchType,
Repository: "sourcegraph/sourcegraph",
Branches: []string{},
}

got := new(bytes.Buffer)
err = v.ExecuteTemplate(got, "repo", struct {
SourcegraphEndpoint string
*streaming.EventRepoMatch
}{
SourcegraphEndpoint: "https://sourcegraph.com",
EventRepoMatch: repo,
})
if err != nil {
t.Fatal(err)
}

autogold.Equal(t, string(got.Bytes()))
}

func TestFileTemplate(t *testing.T) {
v, err := parseTemplate(streamingTemplate)
if err != nil {
t.Fatal(err)
}

file := &streaming.EventFileMatch{
Type: streaming.FileMatchType,
Path: "path/to/file",
Repository: "org/repo",
Branches: nil,
Version: "",
LineMatches: []streaming.EventLineMatch{
{
Line: "foo bar",
LineNumber: 4,
OffsetAndLengths: [][2]int32{{4, 3}},
},
},
}

got := new(bytes.Buffer)
err = v.ExecuteTemplate(got, "file", struct {
Query string
*streaming.EventFileMatch
}{
Query: "bar",
EventFileMatch: file,
})
if err != nil {
t.Fatal(err)
}

autogold.Equal(t, string(got.Bytes()))
}

func TestSymbolTemplate(t *testing.T) {
v, err := parseTemplate(streamingTemplate)
if err != nil {
t.Fatal(err)
}

symbol := &streaming.EventSymbolMatch{
Type: streaming.FileMatchType,
Path: "path/to/file",
Repository: "org/repo",
Branches: nil,
Version: "",
Symbols: []streaming.Symbol{{
URL: "github.com/sourcegraph/sourcegraph/-/blob/cmd/frontend/graphqlbackend/search_results.go#L1591:26-1591:35",
Name: "doResults",
ContainerName: "",
Kind: "FUNCTION",
}},
}

got := new(bytes.Buffer)
err = v.ExecuteTemplate(got, "symbol", struct {
SourcegraphEndpoint string
*streaming.EventSymbolMatch
}{
SourcegraphEndpoint: "https://sourcegraph.com",
EventSymbolMatch: symbol,
})
if err != nil {
t.Fatal(err)
}

autogold.Equal(t, string(got.Bytes()))
}

func TestCommitTemplate(t *testing.T) {
v, err := parseTemplate(streamingTemplate)
if err != nil {
t.Fatal(err)
}

commit := &streaming.EventCommitMatch{
Type: streaming.CommitMatchType,
Icon: "",
Label: "[sourcegraph/sourcegraph-atom](/github.com/sourcegraph/sourcegraph-atom) › [Stephen Gutekanst](/github.com/sourcegraph/sourcegraph-atom/-/commit/5b098d7fed963d88e23057ed99d73d3c7a33ad89): [all: release v1.0.5](/github.com/sourcegraph/sourcegraph-atom/-/commit/5b098d7fed963d88e23057ed99d73d3c7a33ad89)^",
URL: "",
Detail: "",
Content: "```COMMIT_EDITMSG\nfoo bar\n```",
Ranges: [][3]int32{
{1, 3, 3},
},
}

got := new(bytes.Buffer)
err = v.ExecuteTemplate(got, "commit", struct {
SourcegraphEndpoint string
*streaming.EventCommitMatch
}{
SourcegraphEndpoint: "https://sourcegraph.com",
EventCommitMatch: commit,
})
if err != nil {
t.Fatal(err)
}

autogold.Equal(t, string(got.Bytes()))
}

func TestProgressTemplate(t *testing.T) {
v, err := parseTemplate(streamingTemplate)
if err != nil {
t.Fatal(err)
}

repoCount := 42
progress := streaming.Progress{
Done: true,
RepositoriesCount: &repoCount,
MatchCount: 17,
DurationMs: 421,
Skipped: nil,
}

got := new(bytes.Buffer)
err = v.ExecuteTemplate(got, "progress", progress)
if err != nil {
t.Fatal(err)
}

autogold.Equal(t, string(got.Bytes()))
}

func TestDiffTemplate(t *testing.T) {
v, err := parseTemplate(streamingTemplate)
if err != nil {
t.Fatal(err)
}

commit := &streaming.EventCommitMatch{
Type: streaming.CommitMatchType,
Icon: "",
Label: "[sourcegraph/sourcegraph-atom](/github.com/sourcegraph/sourcegraph-atom) › [Stephen Gutekanst](/github.com/sourcegraph/sourcegraph-atom/-/commit/5b098d7fed963d88e23057ed99d73d3c7a33ad89): [all: release v1.0.5](/github.com/sourcegraph/sourcegraph-atom/-/commit/5b098d7fed963d88e23057ed99d73d3c7a33ad89)^",
URL: "",
Detail: "",
Content: "```diff\nsrc/data.ts src/data.ts\n@@ -0,0 +11,4 @@\n+ return of<Data>({\n+ title: 'Acme Corp open-source code search',\n+ summary: 'Instant code search across all Acme Corp open-source code.',\n+ githubOrgs: ['sourcegraph'],\n```",
Ranges: [][3]int32{
{4, 44, 6},
},
}

got := new(bytes.Buffer)
err = v.ExecuteTemplate(got, "commit", struct {
SourcegraphEndpoint string
*streaming.EventCommitMatch
}{
SourcegraphEndpoint: "https://sourcegraph.com",
EventCommitMatch: commit,
})
if err != nil {
t.Fatal(err)
}

autogold.Equal(t, string(got.Bytes()))
}
6 changes: 6 additions & 0 deletions cmd/src/testdata/TestCommitTemplate.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
`(https://sourcegraph.com)
sourcegraph/sourcegraph-atom > Stephen Gutekanst : all: release v1.0.5 (1 match)
--------------------------------------------------------------------------------
 oo bar

`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a gostring looks a bit ugly here. Can we rather use autogold.Raw?

11 changes: 11 additions & 0 deletions cmd/src/testdata/TestDiffTemplate.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
`(https://sourcegraph.com)
sourcegraph/sourcegraph-atom > Stephen Gutekanst : all: release v1.0.5 (1 match)
--------------------------------------------------------------------------------
 src/data.ts src/data.ts
@@ -0,0 +11,4 @@
+ return of<Data>({
+ title: 'Acme Corp open-source code search',
+ summary: 'Instant code search across all Acme Corp open-source code.',
+ githubOrgs: ['sourcegraph'],

`
5 changes: 5 additions & 0 deletions cmd/src/testdata/TestFileTemplate.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`org/repo › path/to/file (1 match)
--------------------------------------------------------------------------------
  5 | foo bar

`
2 changes: 2 additions & 0 deletions cmd/src/testdata/TestProgressTemplate.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`✱ 17 results in 421ms from 42 Repositories
`
2 changes: 2 additions & 0 deletions cmd/src/testdata/TestRepoTemplate.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`sourcegraph/sourcegraph (https://sourcegraph.com/sourcegraph/sourcegraph) (1 match)
`
5 changes: 5 additions & 0 deletions cmd/src/testdata/TestSymbolTemplate.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`org/repo › path/to/file (1 match)
--------------------------------------------------------------------------------
doResults(FUNCTION) (https://sourcegraph.com/github.com/sourcegraph/sourcegraph/-/blob/cmd/frontend/graphqlbackend/search_results.go#L1591:26-1591:35)

`
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/efritz/pentimento v0.0.0-20190429011147-ade47d831101
github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.5.2
github.com/google/go-cmp v0.5.4
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.0
github.com/hexops/autogold v1.3.0
github.com/jig/teereadcloser v0.0.0-20181016160506-953720c48e05
github.com/json-iterator/go v1.1.10
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
Expand All @@ -26,9 +27,9 @@ require (
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4
golang.org/x/net v0.0.0-20201021035429-f5854403a974
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/sys v0.0.0-20210218084038-e8e29180ff58
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7
)
Expand Down
Loading