From 9dc0d039d95f4e6f117ec671d3677a240323f84a Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 28 Jan 2024 16:54:23 -0500 Subject: [PATCH 1/3] refactor(python): relocate py shims to module --- commands.go | 36 +++++++++++++++++++++--------------- commands_test.go | 4 ++-- python/shims.go | 12 ++++++++++++ 3 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 python/shims.go diff --git a/commands.go b/commands.go index 1432b0c..581aafb 100644 --- a/commands.go +++ b/commands.go @@ -1,28 +1,25 @@ package main import ( + "maps" "os" cli "v/cli" logger "v/logger" + python "v/python" state "v/state" ) -var DIRECTORIES = []string{ +var stateDirectories = []string{ "cache", "runtimes", "shims", } -var SHIMS = []string{ - "python", - "python3", -} +const defaultFilePermissions = 0775 -const DEFAULT_PERMISSION = 0775 - -func writeShim(shimPath string) error { - shimContent := []byte("#!/bin/bash\n$(v python which --raw) $@") - if err := os.WriteFile(shimPath, shimContent, DEFAULT_PERMISSION); err != nil { +func writeShim(shimPath string, shimCall string) error { + shimContent := []byte("#!/bin/bash\n" + shimCall) + if err := os.WriteFile(shimPath, shimContent, defaultFilePermissions); err != nil { return err } @@ -37,13 +34,22 @@ func Initialize(args []string, flags cli.Flags, currentState state.State) error return nil } - os.Mkdir(state.GetStatePath(), DEFAULT_PERMISSION) - for _, dir := range DIRECTORIES { - os.Mkdir(state.GetStatePath(dir), DEFAULT_PERMISSION) + os.Mkdir(state.GetStatePath(), defaultFilePermissions) + logger.InfoLogger.Printf("Created state directory: %s\n", state.GetStatePath()) + for _, dir := range stateDirectories { + newPath := state.GetStatePath(dir) + os.Mkdir(newPath, defaultFilePermissions) + logger.InfoLogger.Printf("Created %s\n", newPath) } - for _, shim := range SHIMS { - writeShim(state.GetStatePath("shims", shim)) + allShims := map[string]string{} + + maps.Copy(allShims, python.Shims) + + for shimName, shimContent := range allShims { + newShim := state.GetStatePath("shims", shimName) + writeShim(newShim, shimContent) + logger.InfoLogger.Printf("Created shim: %s\n", newShim) } return nil diff --git a/commands_test.go b/commands_test.go index 9ff39ba..b765b7a 100644 --- a/commands_test.go +++ b/commands_test.go @@ -14,7 +14,7 @@ func TestWriteShim(t *testing.T) { os.Mkdir(state.GetStatePath("shims"), 0775) testShimPath := state.GetStatePath("shims", "testshim") - e := writeShim(testShimPath) + e := writeShim(testShimPath, "testcommand") shimContent, _ := ioutil.ReadFile(testShimPath) @@ -22,7 +22,7 @@ func TestWriteShim(t *testing.T) { t.Errorf("Errored while writing shim") } - if !strings.Contains(string(shimContent), "$(v python which --raw) $@") { + if !strings.Contains(string(shimContent), "testcommand") { t.Errorf("Expected shim to contain pass-through via 'which', got %s", shimContent) } diff --git a/python/shims.go b/python/shims.go new file mode 100644 index 0000000..d22762c --- /dev/null +++ b/python/shims.go @@ -0,0 +1,12 @@ +package python + +var pythonShimCall = "$(v python which --raw) $@" + +var pipShimCall = "$(v python which --raw) -m pip $@" + +var Shims = map[string]string{ + "python": pythonShimCall, + "python3": pythonShimCall, + "pip": pipShimCall, + "pip3": pipShimCall, +} From 2b7b63c9b47d08b6f06adb72962eed1c8a63c8c5 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 28 Jan 2024 21:41:12 -0500 Subject: [PATCH 2/3] docs(python): summary notes on the python module --- python/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 python/README.md diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..ff635ae --- /dev/null +++ b/python/README.md @@ -0,0 +1,11 @@ +# Python support + +Managing Python versions is supported via `v python `, see `v help` for a breakdown of what's available. + +## Shims + +Shims are configured in `python/shims.go`. + +## Available versions + +Versions are pulled from [Python's ftp repository](https://www.python.org/ftp/python/) as gzipped tarballs. Any version available where is available for installation via `v python install`. From a11fe69f65fce318042a6bf7bdbc11ce73fb4189 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 28 Jan 2024 21:57:22 -0500 Subject: [PATCH 3/3] test(commands): add shim creation coverage --- commands_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/commands_test.go b/commands_test.go index b765b7a..d624825 100644 --- a/commands_test.go +++ b/commands_test.go @@ -5,6 +5,8 @@ import ( "os" "strings" "testing" + cli "v/cli" + python "v/python" state "v/state" testutils "v/testutils" ) @@ -27,3 +29,53 @@ func TestWriteShim(t *testing.T) { } } + +func TestInitializeCreatesStateDirectories(t *testing.T) { + defer testutils.SetupAndCleanupEnvironment(t)() + + err := Initialize([]string{}, cli.Flags{}, state.State{}) + + if err != nil { + t.Errorf("Unexpected error initializing") + } + + if _, err = os.Stat(state.GetStatePath()); os.IsNotExist(err) { + t.Errorf("Root state directory not found") + } + + if _, err = os.Stat(state.GetStatePath("shims")); os.IsNotExist(err) { + t.Errorf("Shims directory not found") + } + + if _, err = os.Stat(state.GetStatePath("cache")); os.IsNotExist(err) { + t.Errorf("Cache directory not found") + } + + if _, err = os.Stat(state.GetStatePath("runtimes")); os.IsNotExist(err) { + t.Errorf("Runtimes directory not found") + } +} + +func TestInitializeCreatesAllPythonShims(t *testing.T) { + defer testutils.SetupAndCleanupEnvironment(t)() + + err := Initialize([]string{}, cli.Flags{}, state.State{}) + + if err != nil { + t.Errorf("Unexpected error initializing") + } + + expectedShims := python.Shims + + for shimLabel, shimCall := range expectedShims { + shimContent, err := os.ReadFile(state.GetStatePath("shims", shimLabel)) + + if os.IsNotExist(err) { + t.Errorf("%s shim not created", shimLabel) + } + + if !strings.Contains(string(shimContent), shimCall) { + t.Errorf("%s shim does not contain expected call (%s not in %s)", shimLabel, shimCall, shimContent) + } + } +}