diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 70d36dd..fda7275 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -29,4 +29,6 @@ jobs: - name: Install hvm run: go install - name: Test + env: + HVM_GITHUBTOKEN: ${{ secrets.HVM_GITHUBTOKEN }} run: go test ./... diff --git a/cmd/hvm/alias_scripts/bash.sh b/cmd/hvm/alias_scripts/bash.sh index 787c802..b058960 100644 --- a/cmd/hvm/alias_scripts/bash.sh +++ b/cmd/hvm/alias_scripts/bash.sh @@ -1,15 +1,19 @@ # Hugo Version Manager: override path to the hugo executable. hugo() { hvm_show_status=true - if hugo_bin=$(hvm status --printExecPath); then + if hugo_bin=$(hvm status --printExecPathCached); then if [ "${hvm_show_status}" == true ]; then >&2 printf "Hugo version management is enabled in this directory.\\n" >&2 printf "Run 'hvm status' for details, or 'hvm disable' to disable.\\n\\n" fi else - if ! hugo_bin=$(which hugo); then - >&2 printf "Command not found.\\n" - return 1 + if hugo_bin=$(hvm status --printExecPath); then + hvm install --useVersionInDotFile + else + if ! hugo_bin=$(which hugo); then + >&2 printf "Command not found.\\n" + return 1 + fi fi fi "${hugo_bin}" "$@" diff --git a/cmd/hvm/alias_scripts/fish.sh b/cmd/hvm/alias_scripts/fish.sh index 787c802..b058960 100644 --- a/cmd/hvm/alias_scripts/fish.sh +++ b/cmd/hvm/alias_scripts/fish.sh @@ -1,15 +1,19 @@ # Hugo Version Manager: override path to the hugo executable. hugo() { hvm_show_status=true - if hugo_bin=$(hvm status --printExecPath); then + if hugo_bin=$(hvm status --printExecPathCached); then if [ "${hvm_show_status}" == true ]; then >&2 printf "Hugo version management is enabled in this directory.\\n" >&2 printf "Run 'hvm status' for details, or 'hvm disable' to disable.\\n\\n" fi else - if ! hugo_bin=$(which hugo); then - >&2 printf "Command not found.\\n" - return 1 + if hugo_bin=$(hvm status --printExecPath); then + hvm install --useVersionInDotFile + else + if ! hugo_bin=$(which hugo); then + >&2 printf "Command not found.\\n" + return 1 + fi fi fi "${hugo_bin}" "$@" diff --git a/cmd/hvm/alias_scripts/powershell.ps1 b/cmd/hvm/alias_scripts/powershell.ps1 index 1c9f7dc..be018dd 100644 --- a/cmd/hvm/alias_scripts/powershell.ps1 +++ b/cmd/hvm/alias_scripts/powershell.ps1 @@ -1,7 +1,7 @@ # Hugo Version Manager: override path to the hugo executable. function Hugo-Override { Set-Variable -Name "hvm_show_status" -Value $true - Set-Variable -Name "hugo_bin" -Value $(hvm status --printExecPath) + Set-Variable -Name "hugo_bin" -Value $(hvm status --printExecPathCached) If ($hugo_bin) { If ($hvm_show_status) { echo "Hugo version management is enabled in this directory." @@ -9,11 +9,16 @@ function Hugo-Override { } & "$hugo_bin" $args } Else { - Set-Variable -Name "hugo_bin" -Value $((gcm hugo.exe).Path 2> $null) + Set-Variable -Name "hugo_bin" -Value $(hvm status --printExecPath) If ($hugo_bin) { - & "$hugo_bin" $args + hvm install --useVersionInDotFile } Else { - echo "Command not found" + Set-Variable -Name "hugo_bin" -Value $((gcm hugo.exe).Path 2> $null) + If ($hugo_bin) { + & "$hugo_bin" $args + } Else { + echo "Command not found" + } } } } diff --git a/cmd/hvm/alias_scripts/zsh.sh b/cmd/hvm/alias_scripts/zsh.sh index 787c802..b058960 100644 --- a/cmd/hvm/alias_scripts/zsh.sh +++ b/cmd/hvm/alias_scripts/zsh.sh @@ -1,15 +1,19 @@ # Hugo Version Manager: override path to the hugo executable. hugo() { hvm_show_status=true - if hugo_bin=$(hvm status --printExecPath); then + if hugo_bin=$(hvm status --printExecPathCached); then if [ "${hvm_show_status}" == true ]; then >&2 printf "Hugo version management is enabled in this directory.\\n" >&2 printf "Run 'hvm status' for details, or 'hvm disable' to disable.\\n\\n" fi else - if ! hugo_bin=$(which hugo); then - >&2 printf "Command not found.\\n" - return 1 + if hugo_bin=$(hvm status --printExecPath); then + hvm install --useVersionInDotFile + else + if ! hugo_bin=$(which hugo); then + >&2 printf "Command not found.\\n" + return 1 + fi fi fi "${hugo_bin}" "$@" diff --git a/cmd/hvm/cmd.go b/cmd/hvm/cmd.go index d7b2bee..4013a65 100644 --- a/cmd/hvm/cmd.go +++ b/cmd/hvm/cmd.go @@ -33,10 +33,12 @@ import ( // others depend on the user environment. type application struct { CacheDirPath string // path to the application cache directory - ConfigFilePath string // path to the application configuration + ConfigDirPath string // path to the application configuration directory + ConfigFilePath string // path to the application configuration file DefaultDirName string // name of the "default" directory within the application cache directory DefaultDirPath string // path to the "default" directory within the application cache directory DotFileName string // name of the dot file written to the current directory (e.g., .hvm) + DotFilePath string // path to the dot file Name string // name of the application RepositoryName string // name of the GitHub repository without the .git extension RepositoryOwner string // account owner of the GitHub repository @@ -152,12 +154,17 @@ func initApp() { userCacheDir, err := os.UserCacheDir() cobra.CheckErr(err) + userConfigDir, err := os.UserConfigDir() + cobra.CheckErr(err) + wd, err := os.Getwd() cobra.CheckErr(err) App.CacheDirPath = filepath.Join(userCacheDir, App.Name) + App.ConfigDirPath = filepath.Join(userConfigDir, App.Name) App.ConfigFilePath = viper.ConfigFileUsed() App.DefaultDirPath = filepath.Join(userCacheDir, App.Name, App.DefaultDirName) + App.DotFilePath = filepath.Join(wd, App.DotFileName) App.WorkingDir = wd err = os.MkdirAll(App.CacheDirPath, 0777) diff --git a/cmd/hvm/cmd_test.go b/cmd/hvm/cmd_test.go index 1f56d47..3dc1528 100644 --- a/cmd/hvm/cmd_test.go +++ b/cmd/hvm/cmd_test.go @@ -31,4 +31,39 @@ func TestCommands(t *testing.T) { return nil }, }) + testscript.Run(t, testscript.Params{ + Dir: "testscripts/config", + Setup: func(env *testscript.Env) error { + env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN")) + return nil + }, + }) + testscript.Run(t, testscript.Params{ + Dir: "testscripts/install", + Setup: func(env *testscript.Env) error { + env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN")) + return nil + }, + }) + testscript.Run(t, testscript.Params{ + Dir: "testscripts/remove", + Setup: func(env *testscript.Env) error { + env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN")) + return nil + }, + }) + testscript.Run(t, testscript.Params{ + Dir: "testscripts/status", + Setup: func(env *testscript.Env) error { + env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN")) + return nil + }, + }) + testscript.Run(t, testscript.Params{ + Dir: "testscripts/use", + Setup: func(env *testscript.Env) error { + env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN")) + return nil + }, + }) } diff --git a/cmd/hvm/disable.go b/cmd/hvm/disable.go index eae134a..765a20d 100644 --- a/cmd/hvm/disable.go +++ b/cmd/hvm/disable.go @@ -19,7 +19,6 @@ package cmd import ( "fmt" "os" - "path/filepath" "github.com/jmooring/hvm/pkg/helpers" "github.com/spf13/cobra" @@ -42,18 +41,13 @@ func init() { // disable disables version management in the current directory. func disable() error { - wd, err := os.Getwd() - if err != nil { - return err - } - - exists, err := helpers.Exists(filepath.Join(wd, App.DotFileName)) + exists, err := helpers.Exists(App.DotFilePath) if err != nil { return err } if exists { - err := os.Remove(filepath.Join(wd, App.DotFileName)) + err := os.Remove(App.DotFilePath) if err != nil { return err } diff --git a/cmd/hvm/install.go b/cmd/hvm/install.go index 1d64a9a..b37ecd0 100644 --- a/cmd/hvm/install.go +++ b/cmd/hvm/install.go @@ -41,33 +41,36 @@ To use this version when version management is disabled in the current directory, the cache "default" directory must be in your PATH. If it is not, you will be prompted to add it when installation is complete.`, Run: func(cmd *cobra.Command, args []string) { - err := install() + useVersionInDotFile, err := cmd.Flags().GetBool("useVersionInDotFile") + cobra.CheckErr(err) + + err = install(useVersionInDotFile) cobra.CheckErr(err) }, } -var useVersionInDotFile bool - func init() { rootCmd.AddCommand(installCmd) - installCmd.Flags().BoolVar(&useVersionInDotFile, "useVersionInDotFile", false, "Install the version specified by the "+App.DotFileName+" file\nin the current directory") + installCmd.Flags().Bool("useVersionInDotFile", false, "Install the version specified by the "+App.DotFileName+" file\nin the current directory") } // install sets the version of the Hugo executable to use when version // management is disabled in the current directory. -func install() error { +// +//gocyclo:ignore +func install(useVersionInDotFile bool) error { repo := newRepository() asset := newAsset() if useVersionInDotFile { - version, err := getVersionFromDotFile(filepath.Join(App.WorkingDir, App.DotFileName)) + version, err := getVersionFromDotFile(App.DotFilePath) if err != nil { return err } - asset.tag = version - if asset.tag == "" { + if version == "" { return fmt.Errorf("the current directory does not contain an " + App.DotFileName + " file") } + asset.tag = version } else { msg := "Select a version to use when version management is disabled" err := repo.selectTag(asset, msg) diff --git a/cmd/hvm/reset.go b/cmd/hvm/reset.go index fbff7e9..ece44ef 100644 --- a/cmd/hvm/reset.go +++ b/cmd/hvm/reset.go @@ -60,7 +60,7 @@ func reset() error { if err != nil { return err } - err = os.RemoveAll(App.ConfigFilePath) + err = os.RemoveAll(App.ConfigDirPath) if err != nil { return err } diff --git a/cmd/hvm/status.go b/cmd/hvm/status.go index 627bf57..76e5204 100644 --- a/cmd/hvm/status.go +++ b/cmd/hvm/status.go @@ -38,44 +38,101 @@ var statusCmd = &cobra.Command{ Long: `Display a list of cached assets, the size of the cache, and the cache location. The "default" directory created by the "install" command is excluded.`, Run: func(cmd *cobra.Command, args []string) { - err := status() + err := status(cmd) cobra.CheckErr(err) }, } -var printExecPath bool - func init() { rootCmd.AddCommand(statusCmd) - statusCmd.Flags().BoolVar(&printExecPath, "printExecPath", false, "Print the path to the Hugo executable if version\nmanagement is enabled in the current directory") + statusCmd.Flags().Bool("printExecPath", false, `Based on the version specified in the `+App.DotFileName+` file, +print the path to Hugo executable, otherwise +return exit code 1. This path is not verified, and the +executable may not exist.`) + statusCmd.Flags().Bool("printExecPathCached", false, `Based on the version specified in the `+App.DotFileName+` file, +print the path to Hugo executable if cached, otherwise +return exit code 1.`) } // status displays a list of cached assets, the size of the cache, and the // cache location. The "default" directory created by the "install" command is // excluded. -func status() error { - wd, err := os.Getwd() +// +//gocyclo:ignore + +func status(cmd *cobra.Command) error { + printExecPath, err := cmd.Flags().GetBool("printExecPath") + if err != nil { + return err + } + + printExecPathCached, err := cmd.Flags().GetBool("printExecPathCached") + if err != nil { + return err + } + + version, err := getVersionFromDotFile(App.DotFilePath) if err != nil { return err } - version, err := getVersionFromDotFile(filepath.Join(wd, App.DotFileName)) + execPath := filepath.Join(App.CacheDirPath, version, getExecName()) + execPathExists, err := helpers.Exists(execPath) if err != nil { return err } + // Prints exec path, else exit code 1. if printExecPath { if version == "" { os.Exit(1) } else { - fmt.Println(filepath.Join(App.CacheDirPath, version, getExecName())) + fmt.Println(execPath) + os.Exit(0) } - return nil - } else { + } + + // Prints the exec path if the exec is cached, else exit code 1. + if printExecPathCached { if version == "" { - fmt.Println("Version management is disabled in the current directory.") + os.Exit(1) } else { + if execPathExists { + println(execPath) + os.Exit(0) + } else { + os.Exit(1) + } + } + } + + if version == "" { + fmt.Println("Version management is disabled in the current directory.") + } else { + if execPathExists { fmt.Printf("The current directory is configured to use Hugo %s.\n", version) + } else { + var r string + fmt.Printf("The %s file in the current directory refers to a \n", App.DotFileName) + fmt.Printf("Hugo version (%s) that is not cached.\n", version) + fmt.Println() + for { + fmt.Printf("Would you like to install it now? (Y/n): ") + fmt.Scanln(&r) + if len(r) == 0 || strings.ToLower(string(r[0])) == "y" { + install(true) + fmt.Println() + break + } + if strings.ToLower(string(r[0])) == "n" { + err = disable() + if err != nil { + return err + } + fmt.Println() + break + } + } } } @@ -167,10 +224,9 @@ func getVersionFromDotFile(path string) (string, error) { return "", err } - dotHvmContent := strings.TrimSpace(buf.String()) - - theFix := fmt.Sprintf("run `%[1]s use` to select a version, or `%[1]s disable` to remove the file", App.Name) + theFix := fmt.Sprintf("run \"%[1]s use\" to select a version, or \"%[1]s disable\" to remove the file", App.Name) + dotHvmContent := strings.TrimSpace(buf.String()) if dotHvmContent == "" { return "", fmt.Errorf("the %s file in the current directory is empty: %s", App.DotFileName, theFix) } @@ -181,15 +237,5 @@ func getVersionFromDotFile(path string) (string, error) { return "", fmt.Errorf("the %s file in the current directory has an invalid format: %s", App.DotFileName, theFix) } - exists, err = helpers.Exists(filepath.Join(App.CacheDirPath, dotHvmContent, getExecName())) - if err != nil { - return "", err - } - if !exists { - if !useVersionInDotFile { - return "", fmt.Errorf("the %s file in the current directory contains an invalid version (%s): %s", App.DotFileName, dotHvmContent, theFix) - } - } - return (dotHvmContent), nil } diff --git a/cmd/hvm/testscripts/clean.txtar b/cmd/hvm/testscripts/clean.txtar new file mode 100644 index 0000000..c601534 --- /dev/null +++ b/cmd/hvm/testscripts/clean.txtar @@ -0,0 +1,41 @@ +# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir) +[darwin] env HOME=home +[darwin] mkdir "$HOME/Library/Caches" +[darwin] mkdir "$HOME/Library/Application Support" +[linux] env XDG_CACHE_HOME=cache +[linux] env XDG_CONFIG_HOME=config +[windows] env LocalAppData=cache +[windows] env AppData=config + +# Test 1: answer no +stdin input-no.txt +exec hvm clean +stdout 'This will delete cached versions of the Hugo executable.' +stdout 'Are you sure you want to clean the cache\? \(y/N\): ' +stdout 'Canceled' +[darwin] exists home/Library/Caches/hvm/v0.118.0/hugo +[linux] exists cache/hvm/v0.118.0/hugo +[windows] exists cache\\hvm\\v0.118.0\\hugo.exe + + +# Test 2: answer yes +stdin input-yes.txt +exec hvm clean +stdout 'This will delete cached versions of the Hugo executable.' +stdout 'Are you sure you want to clean the cache\? \(y/N\): ' +stdout 'Cache cleaned.' +[darwin] ! exists home/Library/Caches/hvm/v0.118.0 +[linux] ! exists cache/hvm/v0.118.0 +[windows] ! exists cache\\hvm\\v0.118.0 + +# Files +-- input-no.txt -- +n +-- input-yes.txt -- +y +-- home/Library/Caches/hvm/v0.118.0/hugo -- +darwin-exec-bytes +-- cache/hvm/v0.118.0/hugo -- +linux-exec-bytes +-- cache\\hvm\\v0.118.0\\hugo.exe -- +windows-exec-bytes diff --git a/cmd/hvm/testscripts/completion.txt b/cmd/hvm/testscripts/completion.txtar similarity index 100% rename from cmd/hvm/testscripts/completion.txt rename to cmd/hvm/testscripts/completion.txtar diff --git a/cmd/hvm/testscripts/config.txt b/cmd/hvm/testscripts/config/config.txtar similarity index 65% rename from cmd/hvm/testscripts/config.txt rename to cmd/hvm/testscripts/config/config.txtar index 94b0738..f6f6584 100644 --- a/cmd/hvm/testscripts/config.txt +++ b/cmd/hvm/testscripts/config/config.txtar @@ -12,4 +12,6 @@ exec hvm config stdout 'githubToken =' stdout 'numTagsToDisplay = 30' stdout 'sortAscending = true' -stdout 'Configuration file: ' +[darwin] stdout 'Configuration file: .+/home/Library/Application Support/hvm/config.toml' +[linux] stdout 'Configuration file: .+/config/hvm/config.toml' +[windows] stdout 'Configuration file: .+\\config\\hvm\\config.toml' diff --git a/cmd/hvm/testscripts/config_bad_value.txt b/cmd/hvm/testscripts/config/config_bad_value.txtar similarity index 89% rename from cmd/hvm/testscripts/config_bad_value.txt rename to cmd/hvm/testscripts/config/config_bad_value.txtar index 4e56699..92f516a 100644 --- a/cmd/hvm/testscripts/config_bad_value.txt +++ b/cmd/hvm/testscripts/config/config_bad_value.txtar @@ -11,10 +11,10 @@ ! exec hvm config stderr 'Error: configuration: numTagsToDisplay must be a non-zero integer' -# Files for darwin +# Files -- home/Library/Application Support/hvm/config.toml -- numtagstodisplay = "x" - -# Files for linux and windows -- config/hvm/config.toml -- numtagstodisplay = "x" +-- config\\hvm\\config.toml -- +numtagstodisplay = "x" diff --git a/cmd/hvm/testscripts/config_with_env_vars.txt b/cmd/hvm/testscripts/config/config_with_env_vars.txtar similarity index 50% rename from cmd/hvm/testscripts/config_with_env_vars.txt rename to cmd/hvm/testscripts/config/config_with_env_vars.txtar index b4d3aca..f5568d2 100644 --- a/cmd/hvm/testscripts/config_with_env_vars.txt +++ b/cmd/hvm/testscripts/config/config_with_env_vars.txtar @@ -8,12 +8,14 @@ [windows] env AppData=config env HVM_GITHUBTOKEN=my-token -env HVM_NUMTAGSTODISPLAY=42 -env HVM_SORTASCENDING=true +env HVM_NUMTAGSTODISPLAY=67 +env HVM_SORTASCENDING=false # Test exec hvm config -stdout 'githubToken = ''my-token''' -stdout 'numTagsToDisplay = 42' -stdout 'sortAscending = true' -stdout 'Configuration file: ' +stdout 'githubToken =' +stdout 'numTagsToDisplay = 67' +stdout 'sortAscending = false' +[darwin] stdout 'Configuration file: .+/home/Library/Application Support/hvm/config.toml' +[linux] stdout 'Configuration file: .+/config/hvm/config.toml' +[windows] stdout 'Configuration file: .+\\config\\hvm\\config.toml' diff --git a/cmd/hvm/testscripts/config_with_file.txt b/cmd/hvm/testscripts/config/config_with_file.txtar similarity index 61% rename from cmd/hvm/testscripts/config_with_file.txt rename to cmd/hvm/testscripts/config/config_with_file.txtar index 5765c2f..67ab16d 100644 --- a/cmd/hvm/testscripts/config_with_file.txt +++ b/cmd/hvm/testscripts/config/config_with_file.txtar @@ -12,16 +12,17 @@ exec hvm config stdout 'githubToken =' stdout 'numTagsToDisplay = 42' stdout 'sortAscending = false' -stdout 'Configuration file: ' +[darwin] stdout 'Configuration file: .+/home/Library/Application Support/hvm/config.toml' +[linux] stdout 'Configuration file: .+/config/hvm/config.toml' +[windows] stdout 'Configuration file: .+\\config\\hvm\\config.toml' -# Files for darwin +# Files -- home/Library/Application Support/hvm/config.toml -- -githubtoken = "my-token" numtagstodisplay = 42 -sortascending = false - -# Files for linux and windows +sortAscending = false -- config/hvm/config.toml -- -githubtoken = "my-token" numtagstodisplay = 42 -sortascending = false +sortAscending = false +-- config\\hvm\\config.toml -- +numtagstodisplay = 42 +sortAscending = false diff --git a/cmd/hvm/testscripts/status_err_2.txt b/cmd/hvm/testscripts/disable.txtar similarity index 74% rename from cmd/hvm/testscripts/status_err_2.txt rename to cmd/hvm/testscripts/disable.txtar index 915208c..2a9d464 100644 --- a/cmd/hvm/testscripts/status_err_2.txt +++ b/cmd/hvm/testscripts/disable.txtar @@ -8,8 +8,9 @@ [windows] env AppData=config # Test -! exec hvm status -stderr 'the \.hvm file in the current directory has an invalid format' +exec hvm disable +stdout 'Version management has been disabled in the current directory.' +! exists '.hvm' -- .hvm -- -zzz +v0.118.2 diff --git a/cmd/hvm/testscripts/gen.txt b/cmd/hvm/testscripts/gen.txtar similarity index 70% rename from cmd/hvm/testscripts/gen.txt rename to cmd/hvm/testscripts/gen.txtar index 5027156..2f7ad12 100644 --- a/cmd/hvm/testscripts/gen.txt +++ b/cmd/hvm/testscripts/gen.txtar @@ -17,11 +17,23 @@ stdout 'Generate an alias function for the specified shell.' exec hvm gen alias bash stdout '# Hugo Version Manager: override path to the hugo executable.' +exec hvm gen alias bash --help +stdout 'Generate an alias function for the bash shell.' + exec hvm gen alias fish stdout '# Hugo Version Manager: override path to the hugo executable.' +exec hvm gen alias fish --help +stdout 'Generate an alias function for the fish shell.' + exec hvm gen alias powershell stdout '# Hugo Version Manager: override path to the hugo executable.' +exec hvm gen alias powershell --help +stdout 'Generate an alias function for Windows PowerShell.' + exec hvm gen alias zsh stdout '# Hugo Version Manager: override path to the hugo executable.' + +exec hvm gen alias zsh --help +stdout 'Generate an alias function for the zsh shell.' diff --git a/cmd/hvm/testscripts/help.txt b/cmd/hvm/testscripts/help.txtar similarity index 100% rename from cmd/hvm/testscripts/help.txt rename to cmd/hvm/testscripts/help.txtar diff --git a/cmd/hvm/testscripts/hvm.txt b/cmd/hvm/testscripts/hvm.txtar similarity index 100% rename from cmd/hvm/testscripts/hvm.txt rename to cmd/hvm/testscripts/hvm.txtar diff --git a/cmd/hvm/testscripts/install.txt b/cmd/hvm/testscripts/install.txt deleted file mode 100644 index 50fd2b7..0000000 --- a/cmd/hvm/testscripts/install.txt +++ /dev/null @@ -1,20 +0,0 @@ -# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir) -[darwin] env HOME=home -[darwin] mkdir "$HOME/Library/Caches" -[darwin] mkdir "$HOME/Library/Application Support" -[linux] env XDG_CACHE_HOME=cache -[linux] env XDG_CONFIG_HOME=config -[windows] env LocalAppData=cache -[windows] env AppData=config - -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - -# Test -stdin input.txt -exec hvm install -stdout 'Downloading v.*\.\.\. done.' - -# Files --- input.txt -- -1 diff --git a/cmd/hvm/testscripts/install/install.txtar b/cmd/hvm/testscripts/install/install.txtar new file mode 100644 index 0000000..23e64f5 --- /dev/null +++ b/cmd/hvm/testscripts/install/install.txtar @@ -0,0 +1,30 @@ +# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir) +[darwin] env HOME=home +[darwin] mkdir "$HOME/Library/Caches" +[darwin] mkdir "$HOME/Library/Application Support" +[linux] env XDG_CACHE_HOME=cache +[linux] env XDG_CONFIG_HOME=config +[windows] env LocalAppData=cache +[windows] env AppData=config + +# Test +env HVM_NUMTAGSTODISPLAY=-1 +stdin input.txt +exec hvm install +stdout 'Select a version to use when version management is disabled: ' +stdout 'Downloading v0.54.0\.\.\. done.' +stdout 'Installation of v0.54.0 complete.' +[darwin] stdout 'Please add home/Library/Caches/hvm/default to the PATH environment variable.' +[linux] stdout 'Please add cache/hvm/default to the PATH environment variable.' +[windows] stdout 'Please add cache\\hvm\\default to the PATH environment variable.' +stdout 'Open a new terminal after making the change.' +[darwin] exists 'home/Library/Caches/hvm/v0.54.0/hugo' +[darwin] exists 'home/Library/Caches/hvm/default/hugo' +[linux] exists 'cache/hvm/v0.54.0/hugo' +[linux] exists 'cache/hvm/default/hugo' +[windows] exists 'cache\\hvm\\v0.54.0\\hugo.exe' +[windows] exists 'cache\\hvm\\default\\hugo.exe' + +# Files +-- input.txt -- +1 diff --git a/cmd/hvm/testscripts/install_use_dot_file_fail.txt b/cmd/hvm/testscripts/install/install_use_dot_file_fail.txtar similarity index 86% rename from cmd/hvm/testscripts/install_use_dot_file_fail.txt rename to cmd/hvm/testscripts/install/install_use_dot_file_fail.txtar index e37607a..3e5dd95 100644 --- a/cmd/hvm/testscripts/install_use_dot_file_fail.txt +++ b/cmd/hvm/testscripts/install/install_use_dot_file_fail.txtar @@ -7,9 +7,6 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test ! exec hvm install --useVersionInDotFile stderr 'Error: the current directory does not contain an \.hvm file' diff --git a/cmd/hvm/testscripts/install/install_use_dot_file_pass.txtar b/cmd/hvm/testscripts/install/install_use_dot_file_pass.txtar new file mode 100644 index 0000000..9ea6fe1 --- /dev/null +++ b/cmd/hvm/testscripts/install/install_use_dot_file_pass.txtar @@ -0,0 +1,28 @@ +# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir) +[darwin] env HOME=home +[darwin] mkdir "$HOME/Library/Caches" +[darwin] mkdir "$HOME/Library/Application Support" +[linux] env XDG_CACHE_HOME=cache +[linux] env XDG_CONFIG_HOME=config +[windows] env LocalAppData=cache +[windows] env AppData=config + +# Test +exec hvm install --useVersionInDotFile +stdout 'Downloading v0.118.2... done.' +stdout 'Installation of v0.118.2 complete.' +[darwin] stdout 'Please add home/Library/Caches/hvm/default to the PATH environment variable.' +[linux] stdout 'Please add cache/hvm/default to the PATH environment variable.' +[windows] stdout 'Please add cache\\hvm\\default to the PATH environment variable.' +stdout 'Open a new terminal after making the change.' +[darwin] exists 'home/Library/Caches/hvm/default/hugo' +[darwin] exists 'home/Library/Caches/hvm/v0.118.2/hugo' +[linux] exists 'cache/hvm/default/hugo' +[linux] exists 'cache/hvm/v0.118.2/hugo' +[windows] exists 'cache\\hvm\\default\\hugo.exe' +[windows] exists 'cache\\hvm\\v0.118.2\\hugo.exe' + + +# Files +-- .hvm -- +v0.118.2 diff --git a/cmd/hvm/testscripts/install_use_dot_file_pass.txt b/cmd/hvm/testscripts/install_use_dot_file_pass.txt deleted file mode 100644 index 1b59c19..0000000 --- a/cmd/hvm/testscripts/install_use_dot_file_pass.txt +++ /dev/null @@ -1,20 +0,0 @@ -# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir) -[darwin] env HOME=home -[darwin] mkdir "$HOME/Library/Caches" -[darwin] mkdir "$HOME/Library/Application Support" -[linux] env XDG_CACHE_HOME=cache -[linux] env XDG_CONFIG_HOME=config -[windows] env LocalAppData=cache -[windows] env AppData=config - -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - -# Test -exec hvm install --useVersionInDotFile -stdout 'Downloading v0.54.0... done.' -stdout 'Installation of v0.54.0 complete.' - -# Files --- .hvm -- -v0.54.0 diff --git a/cmd/hvm/testscripts/remove.txt b/cmd/hvm/testscripts/remove/remove.txtar similarity index 81% rename from cmd/hvm/testscripts/remove.txt rename to cmd/hvm/testscripts/remove/remove.txtar index 1e6077d..035845f 100644 --- a/cmd/hvm/testscripts/remove.txt +++ b/cmd/hvm/testscripts/remove/remove.txtar @@ -10,6 +10,9 @@ # Test exec hvm remove stdout 'Default version removed.' +[darwin] ! exists 'Library/Caches/hvm/default' +[linux] ! exists 'cache/hvm/default' +[windows] ! exists 'cache/hvm/default' # Files -- home/Library/Caches/hvm/default/hugo -- diff --git a/cmd/hvm/testscripts/remove_nothing_to_remove.txt b/cmd/hvm/testscripts/remove/remove_nothing_to_remove.txtar similarity index 100% rename from cmd/hvm/testscripts/remove_nothing_to_remove.txt rename to cmd/hvm/testscripts/remove/remove_nothing_to_remove.txtar diff --git a/cmd/hvm/testscripts/reset.txt b/cmd/hvm/testscripts/reset.txt deleted file mode 100644 index 4b294db..0000000 --- a/cmd/hvm/testscripts/reset.txt +++ /dev/null @@ -1,25 +0,0 @@ -# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir) -[darwin] env HOME=home -[darwin] mkdir "$HOME/Library/Caches" -[darwin] mkdir "$HOME/Library/Application Support" -[linux] env XDG_CACHE_HOME=cache -[linux] env XDG_CONFIG_HOME=config -[windows] env LocalAppData=cache -[windows] env AppData=config - -[darwin] skip -[windows] skip - -# Test -stdin input.txt -exec hvm reset -! exists cache/v0.118.0/hugo -! exists config/hvm/config.toml - -# Files --- input.txt -- -y --- cache/hvm/v0.118.0/hugo -- -linux-exec-bytes --- config/hvm/config.toml -- -numtagstodisplay = 42 diff --git a/cmd/hvm/testscripts/reset.txtar b/cmd/hvm/testscripts/reset.txtar new file mode 100644 index 0000000..ce56b37 --- /dev/null +++ b/cmd/hvm/testscripts/reset.txtar @@ -0,0 +1,48 @@ +# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir) +[darwin] env HOME=home +[darwin] mkdir "$HOME/Library/Caches" +[darwin] mkdir "$HOME/Library/Application Support" +[linux] env XDG_CACHE_HOME=cache +[linux] env XDG_CONFIG_HOME=config +[windows] env LocalAppData=cache +[windows] env AppData=config + +# Test 1: answer no +stdin input-no.txt +exec hvm reset +stdout 'This will reset the configuration and remove the cache directory.' +stdout 'Are you sure you want to do this\? \(y/N\): ' +stdout 'Canceled.' + +# Test 2: answer yes +stdin input-yes.txt +exec hvm reset +stdout 'This will reset the configuration and remove the cache directory.' +stdout 'Are you sure you want to do this\? \(y/N\): ' +stdout 'Version management has been disabled in the current directory.' +[darwin] ! exists home/Library/Caches/hvm +[darwin] ! exists home/Library/Application Support/hvm" +[linux] ! exists cache/hvm +[linux] ! exists config/hvm +[windows] ! exists cache\\hvm +[windows] ! exists config\\hvm + +# Files +-- input-no.txt -- +n +-- input-yes.txt -- +y +-- home/Library/Caches/hvm/v0.118.0/hugo -- +darwin-exec-bytes +-- home/Library/Application Support/hvm/config.toml -- +numtagstodisplay = 42 + +-- cache/hvm/v0.118.0/hugo -- +linux-exec-bytes +-- config/hvm/config.toml -- +numtagstodisplay = 42 + +-- cache\\hvm\\v0.118.0\\hugo.exe -- +windows-exec-bytes +-- config\\hvm\\config.toml -- +numtagstodisplay = 42 diff --git a/cmd/hvm/testscripts/status.txt b/cmd/hvm/testscripts/status/status.txtar similarity index 92% rename from cmd/hvm/testscripts/status.txt rename to cmd/hvm/testscripts/status/status.txtar index 163e12e..54824a1 100644 --- a/cmd/hvm/testscripts/status.txt +++ b/cmd/hvm/testscripts/status/status.txtar @@ -12,32 +12,30 @@ exec hvm status stdout 'The current directory is configured to use Hugo v0.117.0.' stdout 'Cached versions of the Hugo executable:' stdout 'v0.117.0' - exec hvm status --printExecPath [darwin] stdout 'home/Library/Caches/hvm/v0.117.0/hugo' [linux] stdout 'cache/hvm/v0.117.0/hugo' [windows] stdout 'cache\\hvm\\v0.117.0\\hugo.exe' +exec hvm status --printExecPathCached # Test 2: dot file does not exist, cache not empty rm .hvm - exec hvm status stdout 'Version management is disabled in the current directory.' stdout 'Cached versions of the Hugo executable:' stdout 'v0.117.0' - ! exec hvm status --printExecPath +! exec hvm status --printExecPathCached # Test 3: dot file does not exist, cache empty rm home/Library/Caches/hvm/v0.117.0 rm cache/hvm/v0.117.0 rm cache/hvm/v0.117.0 - exec hvm status stdout 'Version management is disabled in the current directory.' stdout 'The cache is empty.' - ! exec hvm status --printExecPath +! exec hvm status --printExecPathCached # Files -- .hvm -- diff --git a/cmd/hvm/testscripts/disable.txt b/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_no.txtar similarity index 54% rename from cmd/hvm/testscripts/disable.txt rename to cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_no.txtar index 51c8623..613da04 100644 --- a/cmd/hvm/testscripts/disable.txt +++ b/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_no.txtar @@ -7,18 +7,19 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test stdin input.txt -exec hvm use exec hvm status -stdout 'The current directory is configured to use Hugo' -stdout 'Cached versions of the Hugo executable' -exec hvm disable +stdout 'The .hvm file in the current directory refers to a' +stdout 'Hugo version \(v0.118.2\) that is not cached.' +stdout 'Would you like to install it now\? \(Y/n\): ' stdout 'Version management has been disabled in the current directory.' -! exists '.hvm' +[darwin] ! exists 'Library/Caches/hvm/default/v0.118.2/hugo' +[linux] ! exists 'cache/hvm/default/v0.118.2/hugo' +[windows] ! exists 'cache\\hvm\\default\\v0.118.2\\hugo.exe' +# Files -- input.txt -- -1 +n +-- .hvm -- +v0.118.2 diff --git a/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_yes.txtar b/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_yes.txtar new file mode 100644 index 0000000..0ddc38d --- /dev/null +++ b/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_yes.txtar @@ -0,0 +1,29 @@ +# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir) +[darwin] env HOME=home +[darwin] mkdir "$HOME/Library/Caches" +[darwin] mkdir "$HOME/Library/Application Support" +[linux] env XDG_CACHE_HOME=cache +[linux] env XDG_CONFIG_HOME=config +[windows] env LocalAppData=cache +[windows] env AppData=config + +# Test +stdin input.txt +exec hvm status +stdout 'The .hvm file in the current directory refers to a' +stdout 'Hugo version \(v0.118.2\) that is not cached.' +stdout 'Would you like to install it now\? \(Y/n\): ' +stdout 'Downloading v0.118.2... done.' +stdout 'Installation of v0.118.2 complete.' +[darwin] exists 'home/Library/Caches/hvm/default/hugo' +[darwin] exists 'home/Library/Caches/hvm/v0.118.2/hugo' +[linux] exists 'cache/hvm/default/hugo' +[linux] exists 'cache/hvm/v0.118.2/hugo' +[windows] exists 'cache\\hvm\\default\\hugo.exe' +[windows] exists 'cache\\hvm\\v0.118.2\\hugo.exe' + +# Files +-- input.txt -- +y +-- .hvm -- +v0.118.2 diff --git a/cmd/hvm/testscripts/status_err_1.txt b/cmd/hvm/testscripts/status/status_err_1.txtar similarity index 100% rename from cmd/hvm/testscripts/status_err_1.txt rename to cmd/hvm/testscripts/status/status_err_1.txtar diff --git a/cmd/hvm/testscripts/status_err_3.txt b/cmd/hvm/testscripts/status/status_err_2.txtar similarity index 100% rename from cmd/hvm/testscripts/status_err_3.txt rename to cmd/hvm/testscripts/status/status_err_2.txtar diff --git a/cmd/hvm/testscripts/clean.txt b/cmd/hvm/testscripts/status/status_print_exec_path_cached.txtar similarity index 61% rename from cmd/hvm/testscripts/clean.txt rename to cmd/hvm/testscripts/status/status_print_exec_path_cached.txtar index 6db1433..865c5c3 100644 --- a/cmd/hvm/testscripts/clean.txt +++ b/cmd/hvm/testscripts/status/status_print_exec_path_cached.txtar @@ -7,20 +7,18 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# Test 1: cache is not empty -stdin input.txt -exec hvm clean -stdout 'This will delete cached versions of the Hugo executable.' -stdout 'Are you sure you want to clean the cache\? \(y/N\): ' -stdout 'Cache cleaned.' +# Test 1: dot file exists, exec exists +exec hvm status --printExecPathCached -# Test 2: cache is empty -exec hvm clean -stdout 'The cache is already empty.' +# Test 2: dot file exists, exec does not exist +rm home/Library/Caches/hvm/v0.117.0 +rm cache/hvm/v0.117.0 +rm cache/hvm/v0.117.0 +! exec hvm status --printExecPathCached # Files --- input.txt -- -y +-- .hvm -- +v0.117.0 -- home/Library/Caches/hvm/v0.117.0/hugo -- darwin-exec-bytes -- cache/hvm/v0.117.0/hugo -- diff --git a/cmd/hvm/testscripts/use.txt b/cmd/hvm/testscripts/use/use.txtar similarity index 58% rename from cmd/hvm/testscripts/use.txt rename to cmd/hvm/testscripts/use/use.txtar index 59e6758..de10a65 100644 --- a/cmd/hvm/testscripts/use.txt +++ b/cmd/hvm/testscripts/use/use.txtar @@ -7,16 +7,16 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test +env HVM_NUMTAGSTODISPLAY=-1 stdin input.txt exec hvm use -stdout 'Downloading v.*\.\.\. done.' - +stdout 'Downloading v0.54.0\.\.\.\ done.' exec hvm status -stdout 'The current directory is configured to use Hugo v.*' +stdout 'The current directory is configured to use Hugo v0.54.0' +[darwin] exists 'home/Library/Caches/hvm/v0.54.0/hugo' +[linux] exists 'cache/hvm/v0.54.0/hugo' +[windows] exists 'cache\\hvm\\v0.54.0\\hugo.exe' # Files -- input.txt -- diff --git a/cmd/hvm/testscripts/use_check_sort_order.txt b/cmd/hvm/testscripts/use/use_check_sort_order.txtar similarity index 91% rename from cmd/hvm/testscripts/use_check_sort_order.txt rename to cmd/hvm/testscripts/use/use_check_sort_order.txtar index 64dafa4..238c1d5 100644 --- a/cmd/hvm/testscripts/use_check_sort_order.txt +++ b/cmd/hvm/testscripts/use/use_check_sort_order.txtar @@ -7,9 +7,6 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test 1 env HVM_NUMTAGSTODISPLAY=-1 env HVM_SORTASCENDING=true diff --git a/cmd/hvm/testscripts/use_invalid_selection.txt b/cmd/hvm/testscripts/use/use_invalid_selection.txtar similarity index 87% rename from cmd/hvm/testscripts/use_invalid_selection.txt rename to cmd/hvm/testscripts/use/use_invalid_selection.txtar index eb7af24..0c59cbb 100644 --- a/cmd/hvm/testscripts/use_invalid_selection.txt +++ b/cmd/hvm/testscripts/use/use_invalid_selection.txtar @@ -7,9 +7,6 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test env HVM_NUMTAGSTODISPLAY=12 stdin input.txt diff --git a/cmd/hvm/testscripts/version.txt b/cmd/hvm/testscripts/version.txtar similarity index 100% rename from cmd/hvm/testscripts/version.txt rename to cmd/hvm/testscripts/version.txtar diff --git a/cmd/hvm/use.go b/cmd/hvm/use.go index 549c2ea..5587570 100644 --- a/cmd/hvm/use.go +++ b/cmd/hvm/use.go @@ -388,12 +388,7 @@ func (a *asset) getExecPath() string { // createDotFile creates an application dot file in the current directory. func (a *asset) createDotFile() error { - wd, err := os.Getwd() - if err != nil { - return err - } - - err = os.WriteFile(filepath.Join(wd, App.DotFileName), []byte(a.tag), 0644) + err := os.WriteFile(App.DotFilePath, []byte(a.tag), 0644) if err != nil { return err }