forked from sourcegraph/src-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_intel_upload_flags_test.go
More file actions
98 lines (84 loc) · 2.78 KB
/
code_intel_upload_flags_test.go
File metadata and controls
98 lines (84 loc) · 2.78 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
package main
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"github.com/sourcegraph/scip/bindings/go/scip"
)
var exampleSCIPIndex = scip.Index{
Metadata: &scip.Metadata{
TextDocumentEncoding: scip.TextEncoding_UTF8,
ToolInfo: &scip.ToolInfo{
Name: "hello",
Version: "1.0.0",
},
},
}
var exampleLSIFString = `{"id":1,"version":"0.4.3","positionEncoding":"utf-8","toolInfo":{"name":"hello","version":"1.0.0"},"type":"vertex","label":"metaData"}
`
func exampleSCIPBytes(t *testing.T) []byte {
bytes, err := proto.Marshal(&exampleSCIPIndex)
if err != nil {
t.Fatal(err)
}
return bytes
}
func createTempSCIPFile(t *testing.T, scipFileName string) (scipFilePath string, lsifFilePath string) {
dir := t.TempDir()
require.NotEqual(t, "", scipFileName)
scipFilePath = filepath.Join(dir, scipFileName)
lsifFilePath = filepath.Join(dir, "dump.lsif")
err := os.WriteFile(scipFilePath, exampleSCIPBytes(t), 0755)
if err != nil {
t.Fatal(err)
}
return scipFilePath, lsifFilePath
}
func assertLSIFOutput(t *testing.T, lsifFile, expectedLSIFString string) {
out := codeintelUploadOutput()
handleSCIP(out)
lsif, err := os.ReadFile(lsifFile)
if err != nil {
t.Fatal(err)
}
obtained := string(lsif)
if obtained != expectedLSIFString {
t.Fatalf("unexpected LSIF output %s", obtained)
}
if lsifFile != codeintelUploadFlags.file {
t.Fatalf("unexpected codeintelUploadFlag.file value %s, expected %s", codeintelUploadFlags.file, lsifFile)
}
}
func TestImplicitlyConvertSCIPIntoLSIF(t *testing.T) {
for _, filename := range []string{"index.scip", "dump.scip", "dump.lsif-typed"} {
_, lsifFile := createTempSCIPFile(t, filename)
codeintelUploadFlags.file = lsifFile
assertLSIFOutput(t, lsifFile, exampleLSIFString)
}
}
func TestImplicitlyIgnoreSCIP(t *testing.T) {
for _, filename := range []string{"index.scip", "dump.scip", "dump.lsif-typed"} {
_, lsifFile := createTempSCIPFile(t, filename)
codeintelUploadFlags.file = lsifFile
os.WriteFile(lsifFile, []byte("hello world"), 0755)
assertLSIFOutput(t, lsifFile, "hello world")
}
}
func TestExplicitlyConvertSCIPIntoGraph(t *testing.T) {
for _, filename := range []string{"index.scip", "dump.scip", "dump.lsif-typed"} {
scipFile, lsifFile := createTempSCIPFile(t, filename)
codeintelUploadFlags.file = scipFile
assertLSIFOutput(t, lsifFile, exampleLSIFString)
}
}
func TestReplaceExtension(t *testing.T) {
require.Panics(t, func() { replaceExtension("foo", ".xyz") })
require.Equal(t, "foo.xyz", replaceExtension("foo.abc", ".xyz"))
}
func TestReplaceBaseName(t *testing.T) {
require.Panics(t, func() { replaceBaseName("mydir", filepath.Join("dir", "file")) })
require.Equal(t, filepath.Join("a", "d.e"),
replaceBaseName(filepath.Join("a", "b.c"), "d.e"))
}