github.com/abemedia/appcast@v0.4.0/pkg/pipe/pipe_test.go (about)

     1  package pipe_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io/fs"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/abemedia/appcast/integrations/apk"
    12  	"github.com/abemedia/appcast/integrations/appinstaller"
    13  	"github.com/abemedia/appcast/integrations/apt"
    14  	"github.com/abemedia/appcast/integrations/sparkle"
    15  	"github.com/abemedia/appcast/integrations/yum"
    16  	"github.com/abemedia/appcast/internal/test"
    17  	"github.com/abemedia/appcast/internal/testsource"
    18  	"github.com/abemedia/appcast/pkg/pipe"
    19  	"github.com/abemedia/appcast/source"
    20  	target "github.com/abemedia/appcast/target/file"
    21  	"github.com/google/go-cmp/cmp"
    22  	"gopkg.in/yaml.v3"
    23  )
    24  
    25  func TestLoad(t *testing.T) {
    26  	tests := []struct {
    27  		desc   string
    28  		config string
    29  		path   string
    30  		mode   fs.FileMode
    31  		err    error
    32  	}{
    33  		{path: "appcast.yml"},
    34  		{path: "appcast.yaml"},
    35  		{path: ".appcast.yml"},
    36  		{path: ".appcast.yaml"},
    37  		{path: filepath.Join(".github", "appcast.yml")},
    38  		{path: filepath.Join(".github", "appcast.yaml")},
    39  		{
    40  			path: "foo.yml",
    41  			err:  errors.New("no config file found"),
    42  		},
    43  		{
    44  			desc: "permission denied",
    45  			path: "appcast.yml",
    46  			err:  errors.New("open appcast.yml: permission denied"),
    47  			mode: 0o200,
    48  		},
    49  		{
    50  			desc:   "invalid yaml",
    51  			config: `*&%^`,
    52  			path:   "appcast.yml",
    53  			err:    errors.New("yaml: did not find expected alphabetic or numeric character"),
    54  		},
    55  		{
    56  			desc:   "non-existent field",
    57  			config: `foo: bar`,
    58  			path:   "appcast.yml",
    59  			err:    &yaml.TypeError{Errors: []string{"line 1: field foo not found in type pipe.config"}},
    60  		},
    61  		{
    62  			desc:   "failed validation",
    63  			config: `version: invalid`,
    64  			path:   "appcast.yml",
    65  			err: &pipe.Error{
    66  				Errors: []string{
    67  					"version must be a valid version constraint",
    68  					"source is a required field",
    69  					"target is a required field",
    70  				},
    71  			},
    72  		},
    73  	}
    74  
    75  	opts := cmp.Options{
    76  		test.ExportAll(),
    77  		test.CompareErrorMessages(),
    78  	}
    79  
    80  	wd, _ := os.Getwd()
    81  	defer os.Chdir(wd)
    82  
    83  	for _, test := range tests {
    84  		if test.desc == "" {
    85  			test.desc = test.path
    86  		}
    87  		if test.config == "" {
    88  			test.config = `
    89  				source:
    90  					type: file
    91  					path: ` + t.TempDir() + `
    92  				target:
    93  					type: file
    94  					path: ` + t.TempDir()
    95  		}
    96  		if test.mode == 0 {
    97  			test.mode = os.ModePerm
    98  		}
    99  
   100  		os.Chdir(t.TempDir())
   101  		os.MkdirAll(filepath.Dir(test.path), os.ModePerm)
   102  		os.WriteFile(test.path, clean(test.config), test.mode)
   103  
   104  		_, err := pipe.Load("")
   105  
   106  		if diff := cmp.Diff(test.err, err, opts); diff != "" {
   107  			t.Errorf("%s:\n%s", test.path, diff)
   108  		}
   109  	}
   110  }
   111  
   112  func TestPipe(t *testing.T) {
   113  	dir := t.TempDir()
   114  	src := testsource.New([]*source.Release{{Version: "v1.0.0"}})
   115  	tgt, _ := target.New(target.Config{Path: dir})
   116  
   117  	tests := []struct {
   118  		desc string
   119  		pipe *pipe.Pipe
   120  		err  error
   121  	}{
   122  		{
   123  			desc: "empty",
   124  			pipe: &pipe.Pipe{},
   125  			err:  errors.New("no integrations configured"),
   126  		},
   127  		{
   128  			desc: "all",
   129  			pipe: &pipe.Pipe{
   130  				Apk: &apk.Config{
   131  					Source: src,
   132  					Target: tgt,
   133  				},
   134  				Appinstaller: &appinstaller.Config{
   135  					Source: src,
   136  					Target: tgt,
   137  				},
   138  				Apt: &apt.Config{
   139  					Source: src,
   140  					Target: tgt,
   141  				},
   142  				Sparkle: &sparkle.Config{
   143  					FileName: "appcast.xml",
   144  					Source:   src,
   145  					Target:   tgt,
   146  				},
   147  				Yum: &yum.Config{
   148  					Source: src,
   149  					Target: tgt,
   150  				},
   151  			},
   152  		},
   153  		{
   154  			desc: "appinstaller error",
   155  			pipe: &pipe.Pipe{
   156  				Appinstaller: &appinstaller.Config{
   157  					Source: source.New(nil),
   158  					Target: tgt,
   159  				},
   160  			},
   161  			err: errors.New("failed to publish App Installer packages: missing source"),
   162  		},
   163  		{
   164  			desc: "apk error",
   165  			pipe: &pipe.Pipe{
   166  				Apk: &apk.Config{
   167  					Source: source.New(nil),
   168  					Target: tgt,
   169  				},
   170  			},
   171  			err: errors.New("failed to publish APK packages: missing source"),
   172  		},
   173  		{
   174  			desc: "apt error",
   175  			pipe: &pipe.Pipe{
   176  				Apt: &apt.Config{
   177  					Source: source.New(nil),
   178  					Target: tgt,
   179  				},
   180  			},
   181  			err: errors.New("failed to publish APT packages: missing source"),
   182  		},
   183  		{
   184  			desc: "sparkle error",
   185  			pipe: &pipe.Pipe{
   186  				Sparkle: &sparkle.Config{
   187  					FileName: "appcast.xml",
   188  					Source:   source.New(nil),
   189  					Target:   tgt,
   190  				},
   191  			},
   192  			err: errors.New("failed to publish Sparkle packages: missing source"),
   193  		},
   194  		{
   195  			desc: "yum error",
   196  			pipe: &pipe.Pipe{
   197  				Yum: &yum.Config{
   198  					Source: source.New(nil),
   199  					Target: tgt,
   200  				},
   201  			},
   202  			err: errors.New("failed to publish YUM packages: missing source"),
   203  		},
   204  	}
   205  
   206  	opts := cmp.Options{
   207  		test.ExportAll(),
   208  		test.CompareErrorMessages(),
   209  	}
   210  
   211  	for _, test := range tests {
   212  		err := test.pipe.Run(context.Background())
   213  		if diff := cmp.Diff(test.err, err, opts); diff != "" {
   214  			t.Errorf("%s:\n%s", test.desc, diff)
   215  		}
   216  	}
   217  }