From e0aad6f936067892e04a463f85ca46689714716c Mon Sep 17 00:00:00 2001 From: Luke Kingland <58986931+lkingland@users.noreply.github.com> Date: Fri, 10 Sep 2021 06:25:30 +0900 Subject: [PATCH] feat: client effective runtimes list (#490) * feat: repositories accessor * feat: repository and templates client api - Templates management api - Repositories management api expansion * fix: nil pointer reference on generate * src: test temp directory name consistency and comment improvements * feat: runtimes accessor --- client.go | 24 ++++++++++++++++++++++++ client_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ repositories.go | 8 ++++---- repositories_test.go | 2 +- 4 files changed, 73 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index 8e60b4cd3..9bd924341 100644 --- a/client.go +++ b/client.go @@ -686,6 +686,30 @@ func (c *Client) Emit(ctx context.Context, endpoint string) error { return c.emitter.Emit(ctx, endpoint) } +// Runtimes available in totality. +// Not all repository/template combinations necessarily exist, +// and further validation is performed when a template+runtime is chosen. +// from a given repository. This is the global list of all available. +// Returned list is unique and sorted. +func (c *Client) Runtimes() ([]string, error) { + runtimes := newSortedSet() + + // Gather all runtimes from all repositories + // into a uniqueness map + repositories, err := c.Repositories.All() + if err != nil { + return []string{}, err + } + for _, repo := range repositories { + for _, runtime := range repo.Runtimes { + runtimes.Add(runtime) + } + } + + // Return a unique, sorted list of runtimes + return runtimes.Items(), nil +} + // sorted set of strings. // // write-optimized and suitable only for fairly small values of N. diff --git a/client_test.go b/client_test.go index 7b557d239..ba3794c67 100644 --- a/client_test.go +++ b/client_test.go @@ -831,6 +831,50 @@ func TestWithConfiguredBuildpacks(t *testing.T) { } } +// TestRuntimes ensures that the total set of runtimes are returned. +func TestRuntimes(t *testing.T) { + // TODO: test when a specific repo override is indicated + // (remote repo which takes precidence over embedded and extended) + + client := fn.New(fn.WithRepositories("testdata/repositories")) + + runtimes, err := client.Runtimes() + if err != nil { + t.Fatal(err) + } + + // Runtimes from `./templates` + `./testdata/repositories` + // Should be unique and sorted. + // + // Note that hard-coding the runtimes list here does add future maintenance + // (test will fail requiring updates when either the builtin set of or test + // set change), but the simplicity and straightforwardness of this + // requirement seems to outweigh the complexity of calculating the list for + // testing, which effectively just recreates the logic within the client. + // Additionally, this list has the benefit of creating a more understandable + // test (a primary goal of course being human communication of libray intent). + // If this is an incorrect assumption, we would need to calculate this + // slice from the contents of ./templates & ./testdata/repositories, taking + // into acount future repository manifests. + expected := []string{ + "customRuntime", + "go", + "node", + "python", + "quarkus", + "rust", + "springboot", + "test", + "typescript", + } + + if !reflect.DeepEqual(runtimes, expected) { + t.Logf("expected: %v", expected) + t.Logf("received: %v", runtimes) + t.Fatal("Runtimes not as expected.") + } +} + // Helpers ---- // USING: Make specified dir. Return deferrable cleanup fn. diff --git a/repositories.go b/repositories.go index f82823c51..b3fe2d0b0 100644 --- a/repositories.go +++ b/repositories.go @@ -98,10 +98,10 @@ func (r *Repositories) Add(name, uri string) (err error) { } // Rename a repository -func (r *Repositories) Rename(old string, new string) error { - oldPath := filepath.Join(r.Path, old) - newPath := filepath.Join(r.Path, new) - return os.Rename(oldPath, newPath) +func (r *Repositories) Rename(from, to string) error { + a := filepath.Join(r.Path, from) + b := filepath.Join(r.Path, to) + return os.Rename(a, b) } // Remove a repository of the given name from the repositories. diff --git a/repositories_test.go b/repositories_test.go index 532f811ff..eab88d0b7 100644 --- a/repositories_test.go +++ b/repositories_test.go @@ -48,7 +48,7 @@ func TestRepositoriesGet(t *testing.T) { // valid should have expected name if repo.Name != "customProvider" { - t.Fatalf("expected 'customProvider' as repository name, got: %v", repo.Name) + t.Fatalf("Expected 'customProvider', got: %v", repo.Name) } }