github.com/wolfi-dev/wolfictl@v0.16.11/pkg/ruby/discover_test.go (about)

     1  package ruby
     2  
     3  import (
     4  	"testing"
     5  
     6  	"chainguard.dev/apko/pkg/build/types"
     7  	"chainguard.dev/melange/pkg/config"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/wolfi-dev/wolfictl/pkg/melange"
    10  )
    11  
    12  func TestIsRubyPackage(t *testing.T) {
    13  	testConfig := config.Configuration{
    14  		Environment: types.ImageConfiguration{
    15  			Contents: types.ImageContents{
    16  				Packages: []string{
    17  					"",
    18  				},
    19  			},
    20  		},
    21  	}
    22  	tests := map[string]struct {
    23  		want  bool
    24  		input string
    25  	}{
    26  		// success cases
    27  		"three_two":     {want: true, input: "ruby-3.2"},
    28  		"three_two_dev": {want: true, input: "ruby-3.2-dev"},
    29  
    30  		// failure cases
    31  		"fail_two_two":            {want: false, input: "ruby-2.2"},
    32  		"fail_three_twowo":        {want: false, input: "ruby-3.22"},
    33  		"fail_threethree_two":     {want: false, input: "ruby-33.2"},
    34  		"fail_two_two_dev":        {want: false, input: "ruby-2.2-dev"},
    35  		"fail_three_twowo_dev":    {want: false, input: "ruby-3.22-dev"},
    36  		"fail_threethree_two_dev": {want: false, input: "ruby-33.2-dev"},
    37  	}
    38  
    39  	o := Options{
    40  		RubyVersion: "3.2",
    41  	}
    42  
    43  	for name, tc := range tests {
    44  		t.Run(name, func(t *testing.T) {
    45  			testConfig.Environment.Contents.Packages[0] = tc.input
    46  			got := o.isRubyPackage(testConfig)
    47  			assert.Equalf(t, tc.want, got, "%s wanted: %s got: %s", name, tc.want, got)
    48  		})
    49  	}
    50  }
    51  
    52  func TestParseRepo(t *testing.T) {
    53  	testConfig := melange.Packages{
    54  		Config: config.Configuration{
    55  			Pipeline: []config.Pipeline{
    56  				{
    57  					// intentionally empty, overwrite for each test
    58  				},
    59  			},
    60  		},
    61  	}
    62  
    63  	uri := "https://github.com/brianmario/charlock_holmes/archive/refs/tags/v${{package.version}}.tar.gz"
    64  	repository := "https://github.com/brianmario/charlock_holmes.git"
    65  	tests := map[string]struct {
    66  		want string
    67  		uses string
    68  		with map[string]string
    69  	}{
    70  		"fetch_uri":     {want: uri, uses: "fetch", with: map[string]string{"uri": uri}},
    71  		"checkout_repo": {want: repository, uses: "git-checkout", with: map[string]string{"repository": repository}},
    72  		"other_junk":    {want: "", uses: "something-else", with: map[string]string{"junk": ""}},
    73  	}
    74  
    75  	for name, tc := range tests {
    76  		t.Run(name, func(t *testing.T) {
    77  			testConfig.Config.Pipeline[0] = config.Pipeline{
    78  				Uses: tc.uses,
    79  				With: tc.with,
    80  			}
    81  			got := parseRepo(&testConfig)
    82  			assert.Equalf(t, tc.want, got, "%s wanted: %s got %s", name, tc.want, got)
    83  		})
    84  	}
    85  }
    86  
    87  func TestParseRef(t *testing.T) {
    88  	pkgVersion := "0.0.0"
    89  	testConfig := melange.Packages{
    90  		Config: config.Configuration{
    91  			Package: config.Package{
    92  				Version: pkgVersion,
    93  			},
    94  			Pipeline: []config.Pipeline{
    95  				{
    96  					// intentionally empty, overwrite for each test
    97  				},
    98  			},
    99  		},
   100  	}
   101  
   102  	tests := map[string]struct {
   103  		want string
   104  		uses string
   105  		with map[string]string
   106  	}{
   107  		"fetch_uri":       {want: "v" + pkgVersion, uses: "fetch", with: map[string]string{"uri": "https://github.com/brianmario/charlock_holmes/archive/refs/tags/v${{package.version}}.tar.gz"}},
   108  		"checkout_tag":    {want: "v" + pkgVersion, uses: "git-checkout", with: map[string]string{"tag": "v${{package.version}}"}},
   109  		"checkout_branch": {want: "v" + pkgVersion, uses: "git-checkout", with: map[string]string{"branch": "v${{package.version}}"}},
   110  		"checkout_junk":   {want: "", uses: "git-checkout", with: map[string]string{"junk": ""}},
   111  		"other_junk":      {want: "", uses: "something-else", with: map[string]string{"junk": ""}},
   112  	}
   113  
   114  	for name, tc := range tests {
   115  		t.Run(name, func(t *testing.T) {
   116  			testConfig.Config.Pipeline[0] = config.Pipeline{
   117  				Uses: tc.uses,
   118  				With: tc.with,
   119  			}
   120  			got := parseRef(&testConfig)
   121  			assert.Equalf(t, tc.want, got, "%s wanted: %s got %s", name, tc.want, got)
   122  		})
   123  	}
   124  }