get.porter.sh/porter@v1.3.0/pkg/pkgmgmt/install_test.go (about)

     1  package pkgmgmt
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestInstallOptions_ValidateName(t *testing.T) {
    12  	t.Run("no name", func(t *testing.T) {
    13  		opts := InstallOptions{}
    14  		err := opts.validateName(nil)
    15  		require.EqualError(t, err, "no name was specified")
    16  	})
    17  	t.Run("name specified", func(t *testing.T) {
    18  		opts := InstallOptions{}
    19  		err := opts.validateName([]string{"thename"})
    20  		require.NoError(t, err)
    21  		assert.Equal(t, "thename", opts.Name, "the package name was not captured")
    22  	})
    23  	t.Run("multiple names specified", func(t *testing.T) {
    24  		opts := InstallOptions{}
    25  		err := opts.validateName([]string{"name1", "name2"})
    26  		require.EqualError(t, err, "only one positional argument may be specified, the name, but multiple were received: [name1 name2]")
    27  	})
    28  }
    29  
    30  func TestInstallOptions_DefaultVersion(t *testing.T) {
    31  	t.Run("none specified", func(t *testing.T) {
    32  		opts := InstallOptions{}
    33  		opts.defaultVersion()
    34  		assert.Equal(t, "latest", opts.Version, "we should default to installing the latest version")
    35  	})
    36  	t.Run("version specified", func(t *testing.T) {
    37  		opts := InstallOptions{Version: "canary"}
    38  		opts.defaultVersion()
    39  		assert.Equal(t, "canary", opts.Version, "defaultVersion should not overwrite the user's choice")
    40  	})
    41  }
    42  
    43  func TestInstallOptions_ValidateFeedURL(t *testing.T) {
    44  	t.Run("feed url unset, mirror unset", func(t *testing.T) {
    45  		opts := InstallOptions{
    46  			PackageType: "mixin",
    47  		}
    48  		err := opts.Validate([]string{"mypkg"})
    49  		require.NoError(t, err)
    50  		wantFeedURL := "https://cdn.porter.sh/mixins/atom.xml"
    51  		assert.Equal(t, wantFeedURL, opts.FeedURL, "fallback to the default feed url when nothing is specified")
    52  
    53  		parsedFeedURL := opts.GetParsedFeedURL()
    54  		assert.Equal(t, wantFeedURL, parsedFeedURL.String(), "validateFeedURL should parse the feed url")
    55  	})
    56  	t.Run("feed url unset, mirror set", func(t *testing.T) {
    57  		opts := InstallOptions{
    58  			PackageType:            "plugin",
    59  			PackageDownloadOptions: PackageDownloadOptions{Mirror: "https://example.com:81/porter"},
    60  		}
    61  		err := opts.Validate([]string{"mypkg"})
    62  		require.NoError(t, err)
    63  		wantFeedURL := "https://example.com:81/porter/plugins/atom.xml"
    64  		assert.Equal(t, wantFeedURL, opts.FeedURL, "fallback to the mirror when nothing is specified")
    65  
    66  		parsedFeedURL := opts.GetParsedFeedURL()
    67  		assert.Equal(t, wantFeedURL, parsedFeedURL.String(), "validateFeedURL should parse the feed url")
    68  	})
    69  	t.Run("user specified feed url", func(t *testing.T) {
    70  		opts := InstallOptions{
    71  			PackageType: "mixin",
    72  			FeedURL:     "https://example.com/atom.xml",
    73  		}
    74  		err := opts.Validate([]string{"mypkg"})
    75  		require.NoError(t, err)
    76  
    77  		parsedFeedURL := opts.GetParsedFeedURL()
    78  		assert.Equal(t, opts.FeedURL, parsedFeedURL.String(), "validateFeedURL should parse the feed url")
    79  	})
    80  	t.Run("user specified url", func(t *testing.T) {
    81  		opts := InstallOptions{
    82  			PackageType: "plugin",
    83  			URL:         "https://example.com/mymixin",
    84  		}
    85  		err := opts.Validate([]string{"mypkg"})
    86  		require.NoError(t, err)
    87  		assert.Nil(t, opts.parsedFeedURL, "validateFeedURL shouldn't try to parse an empty URL")
    88  	})
    89  	t.Run("invalid feed url specified", func(t *testing.T) {
    90  		opts := InstallOptions{
    91  			PackageType: "mixin",
    92  			FeedURL:     "$://example.com",
    93  		}
    94  		err := opts.Validate([]string{"mypkg"})
    95  		assert.Contains(t, err.Error(), fmt.Sprintf("invalid --feed-url %s", opts.FeedURL))
    96  		assert.Contains(t, err.Error(), "first path segment in URL cannot contain colon")
    97  	})
    98  }
    99  
   100  func TestInstallOptions_ValidateURL(t *testing.T) {
   101  	t.Run("url unset, mirror unset", func(t *testing.T) {
   102  		opts := InstallOptions{}
   103  		err := opts.validateURL()
   104  		require.NoError(t, err)
   105  		assert.Nil(t, opts.parsedURL, "validateURL shouldn't try to parse an empty URL")
   106  	})
   107  	t.Run("url unset, mirror set", func(t *testing.T) {
   108  		opts := InstallOptions{}
   109  		err := opts.validateURL()
   110  		require.NoError(t, err)
   111  		assert.Nil(t, opts.parsedURL, "validateURL shouldn't try to parse an empty URL")
   112  	})
   113  	t.Run("url specified", func(t *testing.T) {
   114  		opts := InstallOptions{
   115  			URL: "https://example.com/mymixin",
   116  		}
   117  		err := opts.validateURL()
   118  		require.NoError(t, err)
   119  		parsedURL := opts.parsedURL
   120  		assert.Equal(t, opts.URL, parsedURL.String(), "validateURL should parse the URL")
   121  	})
   122  	t.Run("invalid url specified", func(t *testing.T) {
   123  		opts := InstallOptions{
   124  			URL: "$://example.com",
   125  		}
   126  		err := opts.validateURL()
   127  		assert.Contains(t, err.Error(), fmt.Sprintf("invalid --url %s", opts.URL))
   128  		assert.Contains(t, err.Error(), "first path segment in URL cannot contain colon")
   129  	})
   130  }
   131  
   132  func TestInstallOptions_Validate(t *testing.T) {
   133  	t.Run("mixin", func(t *testing.T) {
   134  		opts := InstallOptions{
   135  			PackageType: "mixin",
   136  		}
   137  		err := opts.Validate([]string{"pkg"})
   138  		require.NoError(t, err, "Validate failed")
   139  		assert.NotEmpty(t, opts.FeedURL, "Validate should have defaulted the feed")
   140  		assert.NotEmpty(t, opts.Version, "Validate should have defaulted the version")
   141  		assert.NotEmpty(t, opts.Mirror, "Validate should have defaulted the mirror")
   142  	})
   143  	t.Run("plugin", func(t *testing.T) {
   144  		opts := InstallOptions{
   145  			PackageType: "mixin",
   146  		}
   147  		err := opts.Validate([]string{"pkg"})
   148  		require.NoError(t, err, "Validate failed")
   149  		assert.NotEmpty(t, opts.FeedURL, "Validate should have defaulted the feed")
   150  		assert.NotEmpty(t, opts.Version, "Validate should have defaulted the version")
   151  		assert.NotEmpty(t, opts.Mirror, "Validate should have defaulted the mirror")
   152  	})
   153  	t.Run("invalid package type", func(t *testing.T) {
   154  		opts := InstallOptions{
   155  			PackageType: "oops",
   156  		}
   157  		err := opts.Validate([]string{"pkg"})
   158  		require.Error(t, err, "Validate should have failed")
   159  		assert.Contains(t, err.Error(), `invalid package type "oops"`)
   160  	})
   161  }