`notebook.dir` should only be parsed from the global configuration file

pull/304/head
Leonardo Mello 1 year ago
parent ad3f63156d
commit 5a5ee1b93a

@ -66,7 +66,7 @@ func NewContainer(version string) (*Container, error) {
return nil, wrap(err)
}
if configPath != "" {
config, err = core.OpenConfig(configPath, config, fs)
config, err = core.OpenConfig(configPath, config, fs, true)
if err != nil {
return nil, wrap(err)
}

@ -258,7 +258,7 @@ func (c GroupConfig) Clone() GroupConfig {
// OpenConfig creates a new Config instance from its TOML representation stored
// in the given file.
func OpenConfig(path string, parentConfig Config, fs FileStorage) (Config, error) {
func OpenConfig(path string, parentConfig Config, fs FileStorage, isGlobal bool) (Config, error) {
// The local config is optional.
exists, err := fs.FileExists(path)
if err == nil && !exists {
@ -270,7 +270,7 @@ func OpenConfig(path string, parentConfig Config, fs FileStorage) (Config, error
return parentConfig, errors.Wrapf(err, "failed to open config file at %s", path)
}
return ParseConfig(content, path, parentConfig)
return ParseConfig(content, path, parentConfig, isGlobal)
}
// ParseConfig creates a new Config instance from its TOML representation.
@ -278,7 +278,7 @@ func OpenConfig(path string, parentConfig Config, fs FileStorage) (Config, error
// for templates.
//
// The parentConfig will be used to inherit default config settings.
func ParseConfig(content []byte, path string, parentConfig Config) (Config, error) {
func ParseConfig(content []byte, path string, parentConfig Config, isGlobal bool) (Config, error) {
wrap := errors.Wrapperf("failed to read config")
config := parentConfig
@ -292,7 +292,11 @@ func ParseConfig(content []byte, path string, parentConfig Config) (Config, erro
// Notebook
notebook := tomlConf.Notebook
if notebook.Dir != "" {
config.Notebook.Dir = opt.NewNotEmptyString(notebook.Dir)
if isGlobal {
config.Notebook.Dir = opt.NewNotEmptyString(notebook.Dir)
} else {
return config, wrap(errors.New("notebook.dir should not be set on local configuration"))
}
}
// Note

@ -10,7 +10,7 @@ import (
)
func TestParseDefaultConfig(t *testing.T) {
conf, err := ParseConfig([]byte(""), ".zk/config.toml", NewDefaultConfig())
conf, err := ParseConfig([]byte(""), ".zk/config.toml", NewDefaultConfig(), true)
assert.Nil(t, err)
assert.Equal(t, conf, Config{
@ -61,7 +61,7 @@ func TestParseDefaultConfig(t *testing.T) {
}
func TestParseInvalidConfig(t *testing.T) {
_, err := ParseConfig([]byte(`;`), ".zk/config.toml", NewDefaultConfig())
_, err := ParseConfig([]byte(`;`), ".zk/config.toml", NewDefaultConfig(), false)
assert.NotNil(t, err)
}
@ -144,7 +144,7 @@ func TestParseComplete(t *testing.T) {
[lsp.diagnostics]
wiki-title = "hint"
dead-link = "none"
`), ".zk/config.toml", NewDefaultConfig())
`), ".zk/config.toml", NewDefaultConfig(), true)
assert.Nil(t, err)
assert.Equal(t, conf, Config{
@ -304,7 +304,7 @@ func TestParseMergesGroupConfig(t *testing.T) {
log-ext = "value"
[group.inherited]
`), ".zk/config.toml", NewDefaultConfig())
`), ".zk/config.toml", NewDefaultConfig(), false)
assert.Nil(t, err)
assert.Equal(t, conf, Config{
@ -403,7 +403,7 @@ func TestParsePreservePropertiesAllowingEmptyValues(t *testing.T) {
[tool]
pager = ""
fzf-preview = ""
`), ".zk/config.toml", NewDefaultConfig())
`), ".zk/config.toml", NewDefaultConfig(), false)
assert.Nil(t, err)
assert.Equal(t, conf.Tool.Pager.IsNull(), false)
@ -412,13 +412,29 @@ func TestParsePreservePropertiesAllowingEmptyValues(t *testing.T) {
assert.Equal(t, conf.Tool.FzfPreview, opt.NewString(""))
}
func TestParseNotebook(t *testing.T) {
toml := `
[notebook]
dir = "/home/user/folder"
`
// Should parse notebook if isGlobal == true
conf, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig(), true)
assert.Nil(t, err)
assert.Equal(t, conf.Notebook.Dir, opt.NewString("/home/user/folder"))
// Should not parse notebook if isGlobal == false
conf, err = ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig(), false)
assert.NotNil(t, err)
assert.Err(t, err, "notebook.dir should not be set on local configuration")
}
func TestParseIDCharset(t *testing.T) {
test := func(charset string, expected Charset) {
toml := fmt.Sprintf(`
[note]
id-charset = "%v"
`, charset)
conf, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig())
conf, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig(), false)
assert.Nil(t, err)
if !cmp.Equal(conf.Note.IDOptions.Charset, expected) {
t.Errorf("Didn't parse ID charset `%v` as expected", charset)
@ -439,7 +455,7 @@ func TestParseIDCase(t *testing.T) {
[note]
id-case = "%v"
`, letterCase)
conf, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig())
conf, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig(), false)
assert.Nil(t, err)
if !cmp.Equal(conf.Note.IDOptions.Case, expected) {
t.Errorf("Didn't parse ID case `%v` as expected", letterCase)
@ -460,7 +476,7 @@ func TestParseMarkdownLinkEncodePath(t *testing.T) {
[format.markdown]
link-format = "%s"
`, format)
conf, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig())
conf, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig(), false)
assert.Nil(t, err)
assert.Equal(t, conf.Format.Markdown.LinkEncodePath, expected)
}
@ -478,7 +494,7 @@ func TestParseLSPDiagnosticsSeverity(t *testing.T) {
wiki-title = "%s"
dead-link = "%s"
`, value, value)
conf, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig())
conf, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig(), false)
assert.Nil(t, err)
assert.Equal(t, conf.LSP.Diagnostics.WikiTitle, expected)
assert.Equal(t, conf.LSP.Diagnostics.DeadLink, expected)
@ -495,7 +511,7 @@ func TestParseLSPDiagnosticsSeverity(t *testing.T) {
[lsp.diagnostics]
wiki-title = "foobar"
`
_, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig())
_, err := ParseConfig([]byte(toml), ".zk/config.toml", NewDefaultConfig(), false)
assert.Err(t, err, "foobar: unknown LSP diagnostic severity - may be none, hint, info, warning or error")
}

@ -64,7 +64,7 @@ func (ns *NotebookStore) Open(path string) (*Notebook, error) {
}
configPath := filepath.Join(path, ".zk/config.toml")
config, err := OpenConfig(configPath, ns.config, ns.fs)
config, err := OpenConfig(configPath, ns.config, ns.fs, false)
if err != nil {
return nil, wrap(err)
}

Loading…
Cancel
Save