Skip to content

Commit

Permalink
tiltfile: add config.main_path, config.main_dir. Fixes #3507 (#3784)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed Sep 17, 2020
1 parent 361b25e commit d8c222c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/tiltfile/config/config.go
Expand Up @@ -81,6 +81,19 @@ func (e *Extension) OnStart(env *starkit.Environment) error {
}

err := env.AddValue("config.tilt_subcommand", starlark.String(e.TiltSubcommand))
if err != nil {
return err
}

err = env.AddValue("config.main_path", starlark.String(env.StartPath()))
if err != nil {
return err
}

err = env.AddValue("config.main_dir", starlark.String(filepath.Dir(env.StartPath())))
if err != nil {
return err
}

return err
}
Expand Down
38 changes: 38 additions & 0 deletions internal/tiltfile/config/config_test.go
Expand Up @@ -459,6 +459,44 @@ print(config.tilt_subcommand)
require.Equal(t, "foo\n", f.PrintOutput())
}

func TestTiltfilePath(t *testing.T) {
f := NewFixture(t, model.UserConfigState{}, "foo")
defer f.TearDown()

f.File("foo/Tiltfile", `
print(config.main_path)
`)
f.File("Tiltfile", `
include('./foo/Tiltfile')
print(config.main_path)
`)

_, err := f.ExecFile("Tiltfile")
require.NoError(t, err)

val := f.JoinPath("Tiltfile")
require.Equal(t, fmt.Sprintf("%s\n%s\n", val, val), f.PrintOutput())
}

func TestTiltfileDir(t *testing.T) {
f := NewFixture(t, model.UserConfigState{}, "foo")
defer f.TearDown()

f.File("foo/Tiltfile", `
print(config.main_dir)
`)
f.File("Tiltfile", `
include('./foo/Tiltfile')
print(config.main_dir)
`)

_, err := f.ExecFile("Tiltfile")
require.NoError(t, err)

val := f.Path()
require.Equal(t, fmt.Sprintf("%s\n%s\n", val, val), f.PrintOutput())
}

func NewFixture(tb testing.TB, userConfigState model.UserConfigState, tiltSubcommand model.TiltSubcommand) *starkit.Fixture {
ext := NewExtension(tiltSubcommand)
ext.UserConfigState = userConfigState
Expand Down
8 changes: 8 additions & 0 deletions internal/tiltfile/starkit/environment.go
Expand Up @@ -47,6 +47,7 @@ type Environment struct {
extensions []Extension
fakeFileSystem map[string]string
loadInterceptors []LoadInterceptor
startPath string

builtinCalls []BuiltinCall
}
Expand All @@ -70,6 +71,11 @@ func (e *Environment) SetArgUnpacker(unpackArgs ArgUnpacker) {
e.unpackArgs = unpackArgs
}

// The absolute path to the entrypoint of the environment.
func (e *Environment) StartPath() string {
return e.startPath
}

// Add a builtin to the environment.
//
// All builtins will be wrapped to invoke OnBuiltinCall on every extension.
Expand Down Expand Up @@ -152,6 +158,8 @@ func (e *Environment) start(path string) (Model, error) {
return Model{}, errors.Wrap(err, "environment#start")
}

e.startPath = path

model := NewModel()
for _, ext := range e.extensions {
sExt, isStateful := ext.(StatefulExtension)
Expand Down

0 comments on commit d8c222c

Please sign in to comment.