Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Command #236

Merged
merged 31 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ce39427
spiked format command
matthewnitschke-wk Apr 11, 2024
0454aa5
working implementation of the test command
matthewnitschke-wk Apr 11, 2024
0a4a2ca
better implementation
matthewnitschke-wk Apr 12, 2024
cf625c3
better implementation
matthewnitschke-wk Apr 12, 2024
ba45ecd
iterations on test command
matthewnitschke-wk Apr 12, 2024
611e3b6
implemented tests for test command
matthewnitschke-wk Apr 13, 2024
3538c23
implemented multiline infastructure and diagnostics test cases
matthewnitschke-wk Apr 13, 2024
3d0c328
improved test output
matthewnitschke-wk Apr 15, 2024
77cdf53
added tests and fixed broken implementations of multiline attribute p…
matthewnitschke-wk Apr 21, 2024
3f8abb9
updated documentation
matthewnitschke-wk Apr 21, 2024
4dee2ec
added documentation for the test wildcard
matthewnitschke-wk Apr 21, 2024
b6528a5
few improvements from initial review
matthewnitschke-wk Apr 22, 2024
7f94917
merged master
matthewnitschke-wk Sep 15, 2024
30dda1a
bumped golang version and updated test_command documentation
matthewnitschke-wk Sep 15, 2024
005eba9
updated documentation for test_command
matthewnitschke-wk Sep 15, 2024
b1f45f7
implemented reprolang tests
matthewnitschke-wk Sep 15, 2024
ff37e14
ran prettier
matthewnitschke-wk Sep 15, 2024
d87fc95
resolved failed unit test
matthewnitschke-wk Sep 15, 2024
9efd41f
resolved markdown test
matthewnitschke-wk Sep 15, 2024
7c64cf0
removed accidentally committed binary
matthewnitschke-wk Sep 15, 2024
7b1bdb3
Clean up test code a little bit
varungandhi-src Sep 26, 2024
a5e0616
Snapshot test the test command outputs
varungandhi-src Sep 26, 2024
7ea45bf
Check that there aren't any unused test files
varungandhi-src Sep 26, 2024
b3ea6fd
Add more helpful docs
varungandhi-src Sep 26, 2024
d223ba1
Put test subcommand below stats
varungandhi-src Sep 26, 2024
58b5af4
Run prettier
varungandhi-src Sep 26, 2024
2b124af
More compact initialization
varungandhi-src Sep 26, 2024
1f328be
Add a few more assertions
varungandhi-src Sep 26, 2024
d254ed6
Minor tweaks to syntax reference
varungandhi-src Sep 26, 2024
e221ec6
Downgrade back to Go 1.20.14
varungandhi-src Sep 26, 2024
7429540
Run prettier
varungandhi-src Sep 26, 2024
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
12 changes: 11 additions & 1 deletion cmd/scip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func commands() []*cli.Command {
print := printCommand()
snapshot := snapshotCommand()
stats := statsCommand()
return []*cli.Command{&lint, &print, &snapshot, &stats}
test := testCommand()
return []*cli.Command{&lint, &print, &snapshot, &stats, &test}
}

//go:embed version.txt
Expand Down Expand Up @@ -74,6 +75,15 @@ func fromFlag(storage *string) *cli.StringFlag {
}
}

func commentSyntaxFlag(storage *string) *cli.StringFlag {
return &cli.StringFlag{
Name: "comment-syntax",
Usage: "Comment syntax to use for snapshot files",
Destination: storage,
Value: "//",
}
}

func projectRootFlag(storage *string) *cli.StringFlag {
return &cli.StringFlag{
Name: "project-root",
Expand Down
115 changes: 115 additions & 0 deletions cmd/scip/main_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package main

import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/hexops/autogold/v2"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"

"github.com/sourcegraph/scip/bindings/go/scip"
"github.com/sourcegraph/scip/bindings/go/scip/testutil"
Expand Down Expand Up @@ -101,3 +104,115 @@ func TestSCIPSnapshots(t *testing.T) {
return snapshots
})
}

func unwrap[T any](v T, err error) func(*testing.T) T {
return func(t *testing.T) T {
require.NoError(t, err)
return v
}
}

func TestSCIPTests(t *testing.T) {
cwd := unwrap(os.Getwd())(t)
testDir := filepath.Join(cwd, "tests", "test_cmd")
testPaths := unwrap(os.ReadDir(testDir))(t)
require.Truef(t, len(testPaths) >= 1, "Expected at least one test case in directory: %v", testDir)

os.Setenv("NO_COLOR", "1")
t.Cleanup(func() {
os.Unsetenv("NO_COLOR")
})

type TestCase struct {
dir string
passOutput autogold.Value
failOutput autogold.Value
}

// To update the snapshot values, run 'go test ./cmd/scip -update'.
testCases := []TestCase{
{"roles",
autogold.Expect("✓ passes.repro (3 assertions)\n"),
autogold.Expect(`✗ fails-wrong-role.repro
Comment on lines +133 to +136
Copy link
Contributor

Choose a reason for hiding this comment

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

@matthewnitschke-wk I've added snapshot tests here for checking the output, so that we don't accidentally regress something (earlier, this test was just checking for pass/fail instead of checking the output).

Failure - row: 0, column: 13
Expected: 'reference reprolang repro_manager roles 1.0.0 fails-wrong-role.repro/hello().'
Actual:
- 'definition reprolang repro_manager roles 1.0.0 fails-wrong-role.repro/hello().'✗ fails-wrong-symbol.repro
Failure - row: 0, column: 13
Expected: 'definition reprolang repro_manager roles 1.0.0 fails-wrong-role.repro/hello2().'
Actual:
- 'definition reprolang repro_manager roles 1.0.0 fails-wrong-symbol.repro/hello().'`),
},
{"ranges",
autogold.Expect("✓ passes.repro (3 assertions)\n"),
autogold.Expect(`✗ fails.repro
Failure - row: 0, column: 10
Expected: 'definition passes.repro/hello().'
Actual:
- No attributes found`),
},
{"diagnostics",
autogold.Expect("✓ passes.repro (2 assertions)\n"),
autogold.Expect(`✗ fails-incorrect-diagnostic.repro
Failure - row: 0, column: 11
Expected: 'diagnostic Warning:'
'THIS IS NOT CORRECT'
Actual:
- 'definition reprolang repro_manager diagnostics 1.0.0 fails-incorrect-diagnostic.repro/deprecatedMethod.'
- 'diagnostic Warning'
'deprecated identifier'✗ fails-no-diagnostic.repro
Failure - row: 0, column: 11
Expected: 'diagnostic Warning:'
'deprecated identifier'
Actual:
- 'definition reprolang repro_manager diagnostics 1.0.0 fails-no-diagnostic.repro/hello().'`),
},
}

for _, testPath := range testPaths {
require.Truef(t, slices.ContainsFunc(testCases, func(testCase TestCase) bool {
return testCase.dir == testPath.Name()
}), "Missing entry in testOutputs for %q", testPath.Name())
}

for _, testCase := range testCases {
var dirEntry os.DirEntry
require.Truef(t, slices.ContainsFunc(testPaths, func(entry os.DirEntry) bool {
if entry.Name() == testCase.dir {
dirEntry = entry
return true
}
return false
}), "Stale entry in testOutputs for %q; did you rename or remove the directory", testCase.dir)

subtestDir := filepath.Join(testDir, dirEntry.Name())
require.Truef(t, dirEntry.IsDir(), "not a directory: %q", subtestDir)

t.Run(testCase.dir, func(t *testing.T) {
sources := unwrap(scip.NewSourcesFromDirectory(subtestDir))(t)
index := unwrap(repro.Index("file:/"+subtestDir, dirEntry.Name(), sources, []*repro.Dependency{}))(t)

var passFiles, failFiles []string
testFiles := unwrap(os.ReadDir(subtestDir))(t)
for _, testFile := range testFiles {
if strings.HasPrefix(testFile.Name(), "passes") {
passFiles = append(passFiles, testFile.Name())
} else if strings.HasPrefix(testFile.Name(), "fails") {
failFiles = append(failFiles, testFile.Name())
} else {
t.Fatalf("Test files must start with 'passes' or 'fails'. Received %v", testFile.Name())
}
}

var passOutput bytes.Buffer
err := testMain(subtestDir, passFiles, index, "#", &passOutput)
require.NoError(t, err)
testCase.passOutput.Equal(t, passOutput.String())

var failOutput bytes.Buffer
err = testMain(subtestDir, failFiles, index, "#", &failOutput)
require.Error(t, err)
testCase.failOutput.Equal(t, failOutput.String())
})
}
}
14 changes: 7 additions & 7 deletions cmd/scip/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ func snapshotCommand() cli.Command {
Description: `The snapshot subcommand generates snapshot files which
can be use for inspecting the output of an index in a
visual way. Occurrences are marked with caret signs (^)
and symbol information.`,
and symbol information.
For testing a SCIP indexer, you can either use this subcommand
along with 'git diff' or equivalent, or you can use the dedicated
'test' subcommand for more targeted checks.
`,
Flags: []cli.Flag{
fromFlag(&snapshotFlags.from),
&cli.StringFlag{
Expand All @@ -44,12 +49,7 @@ and symbol information.`,
Destination: &snapshotFlags.strict,
Value: true,
},
&cli.StringFlag{
Name: "comment-syntax",
Usage: "Comment syntax to use for snapshot files",
Destination: &snapshotFlags.commentSyntax,
Value: "//",
},
commentSyntaxFlag(&snapshotFlags.commentSyntax),
},
Action: func(c *cli.Context) error {
return snapshotMain(snapshotFlags)
Expand Down
Loading
Loading