|
| 1 | +package lockfile |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestGradleParser(t *testing.T) { |
| 10 | + f, err := os.Open("../../testdata/lockfiles/gradle/gradle.lockfile") |
| 11 | + if err != nil { |
| 12 | + t.Fatal(err) |
| 13 | + } |
| 14 | + defer f.Close() |
| 15 | + |
| 16 | + parser := &GradleParser{} |
| 17 | + result, err := parser.Parse(context.Background(), f) |
| 18 | + if err != nil { |
| 19 | + t.Fatal(err) |
| 20 | + } |
| 21 | + |
| 22 | + if result.Type != TypeGradleLock { |
| 23 | + t.Errorf("expected type %s, got %s", TypeGradleLock, result.Type) |
| 24 | + } |
| 25 | + |
| 26 | + if len(result.Packages) != 10 { |
| 27 | + t.Fatalf("expected 10 packages, got %d", len(result.Packages)) |
| 28 | + } |
| 29 | + |
| 30 | + found := make(map[string]Package) |
| 31 | + for _, pkg := range result.Packages { |
| 32 | + found[pkg.Name] = pkg |
| 33 | + } |
| 34 | + |
| 35 | + // Coordinate format: group:artifact. |
| 36 | + if pkg, ok := found["org.springframework:spring-core"]; !ok { |
| 37 | + t.Error("missing spring-core") |
| 38 | + } else if pkg.Version != "6.1.2" { |
| 39 | + t.Errorf("spring-core version: got %s, want 6.1.2", pkg.Version) |
| 40 | + } |
| 41 | + |
| 42 | + // Production dep should not be dev. |
| 43 | + if pkg := found["com.google.guava:guava"]; pkg.Dev { |
| 44 | + t.Error("guava should not be marked dev") |
| 45 | + } |
| 46 | + |
| 47 | + // Test-only deps should be Dev=true. |
| 48 | + if pkg, ok := found["org.junit.jupiter:junit-jupiter-api"]; !ok { |
| 49 | + t.Error("missing junit-jupiter-api") |
| 50 | + } else if !pkg.Dev { |
| 51 | + t.Error("junit-jupiter-api should be marked dev") |
| 52 | + } |
| 53 | + if pkg := found["org.mockito:mockito-core"]; !pkg.Dev { |
| 54 | + t.Error("mockito-core should be marked dev") |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestGradleDevDetection(t *testing.T) { |
| 59 | + tests := []struct { |
| 60 | + configs string |
| 61 | + want bool |
| 62 | + }{ |
| 63 | + {"compileClasspath,runtimeClasspath", false}, |
| 64 | + {"testCompileClasspath,testRuntimeClasspath", true}, |
| 65 | + {"compileClasspath,testCompileClasspath", false}, |
| 66 | + {"testImplementation", true}, |
| 67 | + {"", false}, |
| 68 | + } |
| 69 | + for _, tt := range tests { |
| 70 | + got := isGradleTestOnly(tt.configs) |
| 71 | + if got != tt.want { |
| 72 | + t.Errorf("isGradleTestOnly(%q) = %v, want %v", tt.configs, got, tt.want) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestGradleParserType(t *testing.T) { |
| 78 | + p := &GradleParser{} |
| 79 | + if p.Type() != TypeGradleLock { |
| 80 | + t.Errorf("expected %s, got %s", TypeGradleLock, p.Type()) |
| 81 | + } |
| 82 | + if fnames := p.Filenames(); len(fnames) != 1 || fnames[0] != "gradle.lockfile" { |
| 83 | + t.Errorf("unexpected filenames: %v", fnames) |
| 84 | + } |
| 85 | +} |
0 commit comments