|
| 1 | +package grafanadependency |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/grafana/plugin-validator/pkg/analysis" |
| 8 | + "github.com/grafana/plugin-validator/pkg/analysis/passes/metadata" |
| 9 | + "github.com/grafana/plugin-validator/pkg/testpassinterceptor" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestGrafanaDependency(t *testing.T) { |
| 14 | + for _, tc := range []struct { |
| 15 | + name string |
| 16 | + pluginJSON string |
| 17 | + titleMsg string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "valid grafanaDependency constraint", |
| 21 | + pluginJSON: `{ |
| 22 | + "id": "test-org-app", |
| 23 | + "dependencies": { "grafanaDependency": ">=11.6.0" } |
| 24 | + }`, |
| 25 | + titleMsg: "", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "complex but valid grafanaDependency constraint", |
| 29 | + pluginJSON: `{ |
| 30 | + "id": "test-org-app", |
| 31 | + "dependencies": { "grafanaDependency": ">=11.6.11 <12 || >=12.0.10 <12.1 || >=12.1.7 <12.2 || >=12.2.5" } |
| 32 | + }`, |
| 33 | + titleMsg: "", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "invalid grafanaDependency constraint", |
| 37 | + pluginJSON: `{ |
| 38 | + "id": "test-org-app", |
| 39 | + "dependencies": { "grafanaDependency": ">=invalid2" } |
| 40 | + }`, |
| 41 | + titleMsg: "plugin.json: dependencies.grafanaDependency field has invalid or empty version constraint: \">=invalid2\"", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "empty grafanaDependency", |
| 45 | + pluginJSON: `{ |
| 46 | + "id": "test-org-app", |
| 47 | + "dependencies": { "grafanaDependency": "" } |
| 48 | + }`, |
| 49 | + titleMsg: "plugin.json: dependencies.grafanaDependency field has invalid or empty version constraint: \"\"", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "missing grafanaDependency", |
| 53 | + pluginJSON: `{ |
| 54 | + "id": "test-org-app", |
| 55 | + "dependencies": {"plugins": [ ]} |
| 56 | + }`, |
| 57 | + titleMsg: "plugin.json: dependencies.grafanaDependency field has invalid or empty version constraint: \"\"", |
| 58 | + }, |
| 59 | + } { |
| 60 | + t.Run(tc.name, func(t *testing.T) { |
| 61 | + var interceptor testpassinterceptor.TestPassInterceptor |
| 62 | + pass := &analysis.Pass{ |
| 63 | + RootDir: filepath.Join("./"), |
| 64 | + ResultOf: map[*analysis.Analyzer]interface{}{ |
| 65 | + metadata.Analyzer: []byte(tc.pluginJSON), |
| 66 | + }, |
| 67 | + Report: interceptor.ReportInterceptor(), |
| 68 | + } |
| 69 | + |
| 70 | + _, err := Analyzer.Run(pass) |
| 71 | + require.NoError(t, err) |
| 72 | + if len(tc.titleMsg) > 0 { |
| 73 | + require.Len(t, interceptor.Diagnostics, 1) |
| 74 | + require.Equal( |
| 75 | + t, |
| 76 | + tc.titleMsg, |
| 77 | + interceptor.Diagnostics[0].Title, |
| 78 | + ) |
| 79 | + } else { |
| 80 | + require.Len(t, interceptor.Diagnostics, 0) |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
0 commit comments