github.com/YousefHaggyHeroku/pack@v1.5.5/internal/registry/index_test.go (about)

     1  package registry_test
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/heroku/color"
     8  	"github.com/sclevine/spec"
     9  	"github.com/sclevine/spec/report"
    10  
    11  	"github.com/YousefHaggyHeroku/pack/internal/registry"
    12  	h "github.com/YousefHaggyHeroku/pack/testhelpers"
    13  )
    14  
    15  func TestIndex(t *testing.T) {
    16  	color.Disable(true)
    17  	defer color.Disable(false)
    18  	spec.Run(t, "Index", testIndex, spec.Parallel(), spec.Report(report.Terminal{}))
    19  }
    20  
    21  func testIndex(t *testing.T, when spec.G, it spec.S) {
    22  	when("#IndexPath", func() {
    23  		when("valid", func() {
    24  			for _, scenario := range []struct {
    25  				desc,
    26  				root,
    27  				ns,
    28  				name,
    29  				expectation string
    30  			}{
    31  				{
    32  					desc:        "1 char name",
    33  					root:        "/tmp",
    34  					ns:          "acme",
    35  					name:        "a",
    36  					expectation: filepath.Join("/tmp", "1", "acme_a"),
    37  				},
    38  				{
    39  					desc:        "2 char name",
    40  					root:        "/tmp",
    41  					ns:          "acme",
    42  					name:        "ab",
    43  					expectation: filepath.Join("/tmp", "2", "acme_ab"),
    44  				},
    45  				{
    46  					desc:        "3 char name",
    47  					root:        "/tmp",
    48  					ns:          "acme",
    49  					name:        "abc",
    50  					expectation: filepath.Join("/tmp", "3", "ab", "acme_abc"),
    51  				},
    52  				{
    53  					desc:        "4 char name",
    54  					root:        "/tmp",
    55  					ns:          "acme",
    56  					name:        "abcd",
    57  					expectation: filepath.Join("/tmp", "ab", "cd", "acme_abcd"),
    58  				},
    59  				{
    60  					desc:        "> 4 char name",
    61  					root:        "/tmp",
    62  					ns:          "acme",
    63  					name:        "acmelang",
    64  					expectation: filepath.Join("/tmp", "ac", "me", "acme_acmelang"),
    65  				},
    66  			} {
    67  				scenario := scenario
    68  				it(scenario.desc, func() {
    69  					index, err := registry.IndexPath(scenario.root, scenario.ns, scenario.name)
    70  					h.AssertNil(t, err)
    71  					h.AssertEq(t, index, scenario.expectation)
    72  				})
    73  			}
    74  		})
    75  
    76  		when("invalid", func() {
    77  			for _, scenario := range []struct {
    78  				desc,
    79  				ns,
    80  				name,
    81  				error string
    82  			}{
    83  				{
    84  					desc:  "ns is empty",
    85  					ns:    "",
    86  					name:  "a",
    87  					error: "'namespace' cannot be empty",
    88  				},
    89  				{
    90  					desc:  "name is empty",
    91  					ns:    "a",
    92  					name:  "",
    93  					error: "'name' cannot be empty",
    94  				},
    95  				{
    96  					desc:  "namespace has capital letters",
    97  					ns:    "Acme",
    98  					name:  "buildpack",
    99  					error: "'namespace' contains illegal characters (must match '[a-z0-9\\-.]+')",
   100  				},
   101  				{
   102  					desc:  "name has capital letters",
   103  					ns:    "acme",
   104  					name:  "Buildpack",
   105  					error: "'name' contains illegal characters (must match '[a-z0-9\\-.]+')",
   106  				},
   107  				{
   108  					desc:  "namespace is too long",
   109  					ns:    h.RandString(254),
   110  					name:  "buildpack",
   111  					error: "'namespace' too long (max 253 chars)",
   112  				},
   113  				{
   114  					desc:  "name is too long",
   115  					ns:    "acme",
   116  					name:  h.RandString(254),
   117  					error: "'name' too long (max 253 chars)",
   118  				},
   119  			} {
   120  				scenario := scenario
   121  				when(scenario.desc, func() {
   122  					it("errors", func() {
   123  						_, err := registry.IndexPath("/tmp", scenario.ns, scenario.name)
   124  						h.AssertError(t, err, scenario.error)
   125  					})
   126  				})
   127  			}
   128  		})
   129  	})
   130  }