Skip to content

Commit

Permalink
chore: Add init tests in shellspec
Browse files Browse the repository at this point in the history
  • Loading branch information
lox committed Feb 13, 2025
1 parent 92964a5 commit 146f5fb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

// GlobalState configurable by user to be passed through to Hermit.
type GlobalState struct {
Env envars.Envars `help:"Extra environment variables to apply to environments."`
Env envars.Envars `help:"Extra environment variables to apply to environments."`
UserConfig string `hidden:"" help:"Path to Hermit user configuration file." default:"~/.hermit.hcl" env:"HERMIT_USER_CONFIG"`
}

type cliInterface interface {
Expand Down
2 changes: 1 addition & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func Main(config Config) {
cli = &unactivated{cliBase: common}
}

userConfig, err := LoadUserConfig()
userConfig, err := LoadUserConfig(cli.getGlobalState().UserConfig)
if err != nil {
log.Printf("%s: %s", userConfigPath, err)
}
Expand Down
11 changes: 9 additions & 2 deletions app/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ type UserConfig struct {
}

// LoadUserConfig from disk.
func LoadUserConfig() (UserConfig, error) {
// If configPath is provided, it will be used instead of the default userConfigPath.
func LoadUserConfig(configPath ...string) (UserConfig, error) {
config := UserConfig{}
// always return a valid config on error, with defaults set.
_ = hcl.Unmarshal([]byte{}, &config)
data, err := os.ReadFile(kong.ExpandPath(userConfigPath))

path := userConfigPath
if len(configPath) > 0 {
path = configPath[0]
}

data, err := os.ReadFile(kong.ExpandPath(path))
if os.IsNotExist(err) {
return config, nil
} else if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions it/full/spec/it_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,45 @@ Describe "Hermit"
End
End

Describe "Hermit Init"
Describe "Using init_sources from user config"
It "initializes the environment with sources from hermit.hcl"
# Create a custom hermit.hcl with init_sources.
cat > hermit.hcl <<EOF
init_sources = ["source1", "source2"]
EOF

# Run hermit init with the custom config.
When run command ../../bin/hermit init --user-config hermit.hcl

# Verify that the environment is initialized with the correct sources.
The status should be success
The file ./bin/hermit.hcl should include "source1"
The file ./bin/hermit.hcl should include "source2"
End
End

Describe "Command-line sources overriding user config"
It "initializes the environment with command-line sources"
# Create a custom hermit.hcl with init_sources.
cat > hermit.hcl <<EOF
init_sources = ["source1", "source2"]
EOF
# Run hermit init with additional command-line sources.
When run command ../../bin/hermit init --user-config hermit.hcl --sources source3,source4

# Verify that the environment is initialized with the command-line sources.
The status should be success
The file ./bin/hermit.hcl should include "source3"
The file ./bin/hermit.hcl should include "source4"

# Verify that user config sources are NOT present.
The file ./bin/hermit.hcl should not include "source1"
The file ./bin/hermit.hcl should not include "source2"
End
End
End

Describe "Activating the environment"
It "sets the environment correctly"
When call source ./bin/activate-hermit
Expand Down

0 comments on commit 146f5fb

Please sign in to comment.