|
3 | 3 | // license that can be found in the LICENSE file. |
4 | 4 |
|
5 | 5 | package harness |
| 6 | + |
| 7 | +import ( |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestParseFile(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + filename string |
| 16 | + wantErr bool |
| 17 | + validate func(*testing.T, *spec) |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "valid plugin with binary source", |
| 21 | + filename: "binary_source.yml", |
| 22 | + wantErr: false, |
| 23 | + validate: func(t *testing.T, s *spec) { |
| 24 | + wantSource := "https://github.com/drone-plugins/plugin/releases/download/{{ release }}/plugin-{{ os }}-{{ arch }}.zst" |
| 25 | + wantFallback := "" |
| 26 | + |
| 27 | + if got := s.Run.Binary.Source; got != wantSource { |
| 28 | + t.Errorf("Expected source %q, got %q", wantSource, got) |
| 29 | + } |
| 30 | + if got := s.Run.Binary.FallbackSource; got != wantFallback { |
| 31 | + t.Errorf("Expected fallback source %q, got %q", wantFallback, got) |
| 32 | + } |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "valid plugin with binary source and fallback", |
| 37 | + filename: "binary_source_fallback.yml", |
| 38 | + wantErr: false, |
| 39 | + validate: func(t *testing.T, s *spec) { |
| 40 | + wantSource := "https://github.com/drone-plugins/plugin/releases/download/{{ release }}/plugin-{{ os }}-{{ arch }}.zst" |
| 41 | + wantFallback := "https://mirror.example.com/plugin/releases/download/{{ release }}/plugin-{{ os }}-{{ arch }}.zst" |
| 42 | + |
| 43 | + if got := s.Run.Binary.Source; got != wantSource { |
| 44 | + t.Errorf("Expected source %q, got %q", wantSource, got) |
| 45 | + } |
| 46 | + if got := s.Run.Binary.FallbackSource; got != wantFallback { |
| 47 | + t.Errorf("Expected fallback source %q, got %q", wantFallback, got) |
| 48 | + } |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tt := range tests { |
| 54 | + t.Run(tt.name, func(t *testing.T) { |
| 55 | + // Use testdata directory |
| 56 | + testFile := filepath.Join("testdata", tt.filename) |
| 57 | + got, err := parseFile(testFile) |
| 58 | + if (err != nil) != tt.wantErr { |
| 59 | + t.Errorf("parseFile() error = %v, wantErr %v", err, tt.wantErr) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + if !tt.wantErr { |
| 64 | + tt.validate(t, got) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestParseBinaryWithFallback(t *testing.T) { |
| 71 | + yaml := ` |
| 72 | +run: |
| 73 | + binary: |
| 74 | + source: https://github.com/drone-plugins/drone-meltwater-cache/releases/download/{{ release }}/plugin-{{ os }}-{{ arch }}.zst |
| 75 | + fallback_source: https://backup-mirror.example.com/drone-plugins/drone-meltwater-cache/releases/download/{{ release }}/plugin-{{ os }}-{{ arch }}.zst |
| 76 | +` |
| 77 | + out, err := parseString(yaml) |
| 78 | + if err != nil { |
| 79 | + t.Error(err) |
| 80 | + return |
| 81 | + } |
| 82 | + if got, want := out.Run.Binary.Source, "https://github.com/drone-plugins/drone-meltwater-cache/releases/download/{{ release }}/plugin-{{ os }}-{{ arch }}.zst"; got != want { |
| 83 | + t.Errorf("Want source URL %q, got %q", want, got) |
| 84 | + } |
| 85 | + if got, want := out.Run.Binary.FallbackSource, "https://backup-mirror.example.com/drone-plugins/drone-meltwater-cache/releases/download/{{ release }}/plugin-{{ os }}-{{ arch }}.zst"; got != want { |
| 86 | + t.Errorf("Want fallback source URL %q, got %q", want, got) |
| 87 | + } |
| 88 | +} |
0 commit comments