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 b27f060
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 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
7 changes: 1 addition & 6 deletions app/init_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@ type initCmd struct {
Dir string `arg:"" help:"Directory to create environment in (${default})." default:"${env}" predictor:"dir"`
}

func (i *initCmd) Run(w *ui.UI, config Config) error {
func (i *initCmd) Run(w *ui.UI, config Config, userConfig UserConfig) error {
_, sum, err := GenInstaller(config)
if err != nil {
return errors.WithStack(err)
}

userConfig, err := LoadUserConfig()
if err != nil {
return errors.WithStack(err)
}

sources := i.Sources
if len(sources) == 0 {
w.Tracef("Using init_sources from user config: %v", userConfig.InitSources)
Expand Down
3 changes: 2 additions & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ func Main(config Config) {
cli = &unactivated{cliBase: common}
}

userConfig, err := LoadUserConfig()
userConfigPath := cli.getGlobalState().UserConfig
userConfig, err := LoadUserConfig(userConfigPath)
if err != nil {
log.Printf("%s: %s", userConfigPath, err)
}
Expand Down
7 changes: 3 additions & 4 deletions app/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/cashapp/hermit/errors"
)

const userConfigPath = "~/.hermit.hcl"

var userConfigSchema = func() string {
schema, err := hcl.Schema(&UserConfig{})
if err != nil {
Expand All @@ -33,11 +31,12 @@ type UserConfig struct {
}

// LoadUserConfig from disk.
func LoadUserConfig() (UserConfig, error) {
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))

data, err := os.ReadFile(kong.ExpandPath(configPath))
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 b27f060

Please sign in to comment.