-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add end-to-end tests for json subcommand
- Loading branch information
Showing
2 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/sourcegraph/scip/bindings/go/scip" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
func TestJSONCommand(t *testing.T) { | ||
app := scipApp() | ||
// Redirect CLI writer to a buffer | ||
var buff bytes.Buffer | ||
app.Writer = io.Writer(&buff) | ||
idx := makeIndex([]string{"a"}, stringMap{"f": {"b"}}, stringMap{"f": {"a", "b"}}) | ||
|
||
idx.Metadata = &scip.Metadata{ProjectRoot: "howdy"} | ||
|
||
// Serialise SCIP index | ||
indexBytes, err := proto.Marshal(idx) | ||
|
||
// Write SCIP index to a temp file | ||
dir := os.TempDir() | ||
file, err := ioutil.TempFile(dir, "scip-cli-json-test*.scip") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer os.Remove(file.Name()) | ||
|
||
_, fErr := file.Write(indexBytes) | ||
if fErr != nil { | ||
log.Fatal(fErr) | ||
} | ||
|
||
// Run the JSON command with the temporary file | ||
runErr := app.Run([]string{"scip", "json", "--from", file.Name()}) | ||
if runErr != nil { | ||
log.Fatal(runErr) | ||
} | ||
|
||
type JsonIndex struct { | ||
Metadata struct { | ||
ProjectRoot string `json:"projectRoot"` | ||
} | ||
Documents []struct { | ||
RelativePath string `json:"relativePath"` | ||
} `json:"documents"` | ||
} | ||
|
||
var roundtripResult JsonIndex | ||
|
||
bytes := buff.Bytes() | ||
|
||
jsonErr := json.Unmarshal(bytes, &roundtripResult) | ||
if jsonErr != nil { | ||
log.Fatal(jsonErr) | ||
} | ||
|
||
require.Equal(t, roundtripResult.Metadata.ProjectRoot, "howdy") | ||
require.Equal(t, len(roundtripResult.Documents), 1) | ||
require.Equal(t, roundtripResult.Documents[0].RelativePath, "f") | ||
} |