Add runtime:elvish-path

Resolves #1423
This commit is contained in:
Kurtis Rader 2022-06-25 20:56:15 -07:00
parent 551e246d96
commit 3239c38764
3 changed files with 28 additions and 0 deletions

View File

@ -35,3 +35,5 @@ and compiled, even if it is not executed:
- A new type of interactive abbreviation: `edit:command-abbr`
([#1472](https://b.elv.sh/1472)).
- A new `runtime:elvish-path` variable ([#1423](https://b.elv.sh/1423)).

View File

@ -2,6 +2,8 @@
package runtime
import (
"os"
"src.elv.sh/pkg/eval"
"src.elv.sh/pkg/eval/vals"
"src.elv.sh/pkg/eval/vars"
@ -38,6 +40,24 @@ import (
//
// @cf $runtime:rc-path
//elvdoc:var elvish-path
//
// The path to the elvish binary. If that could not be determined the value will be the empty
// string.
//
// This is read-only.
func elvishPath() any {
// TODO: Decide what to do if os.Executable returns an error. It appears that all platforms
// return an empty string. Which makes sense and is probably good enough for our purposes.
// Nonetheless, we explicitly encode that logic rather than rely on the behavior of
// os.Executable. TBD is whether we should instead raise an exception.
if binPath, err := os.Executable(); err == nil {
return binPath
}
return ""
}
// Ns returns the namespace for the runtime: module.
//
// All the public properties of the Evaler should be set before this function is
@ -45,6 +65,7 @@ import (
func Ns(ev *eval.Evaler) *eval.Ns {
return eval.BuildNsNamed("runtime").
AddVars(map[string]vars.Var{
"elvish-path": vars.FromGet(elvishPath),
"lib-dirs": vars.NewReadOnly(vals.MakeListSlice(ev.LibDirs)),
"rc-path": vars.NewReadOnly(nonEmptyOrNil(ev.RcPath)),
"effective-rc-path": vars.NewReadOnly(nonEmptyOrNil(ev.EffectiveRcPath)),

View File

@ -1,6 +1,7 @@
package runtime
import (
"os"
"testing"
"src.elv.sh/pkg/eval"
@ -18,10 +19,14 @@ func TestRuntime(t *testing.T) {
ev.ExtendGlobal(eval.BuildNs().AddNs("runtime", Ns(ev)))
}
elvishPath, _ := os.Executable()
evaltest.TestWithSetup(t, setup,
That("put $runtime:lib-dirs").Puts(vals.MakeList("/lib/1", "/lib/2")),
That("put $runtime:rc-path").Puts("/path/to/rc.elv"),
That("put $runtime:effective-rc-path").Puts("/path/to/effective/rc.elv"),
That(`put $runtime:elvish-path`).Puts(elvishPath),
)
}