From d67ce731ed32586edb913b5752a0a1fdaedc718b Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Wed, 3 Apr 2024 03:04:42 +0200 Subject: [PATCH 1/4] Don't initialize plugins in buffer test and rtfiles test Adding InitPlugins() to tests has caused noisy error logs when running the buffer_test.go test (although the test result is still PASS): 2024/03/23 15:14:30 Plugin does not exist: autoclose at autoclose : &{autoclose autoclose [runtime/plugins/autoclose/autoclose.lua] false true} 2024/03/23 15:14:30 Plugin does not exist: comment at comment : &{comment comment [runtime/plugins/comment/comment.lua] false true} 2024/03/23 15:14:30 Plugin does not exist: diff at diff : &{diff diff [runtime/plugins/diff/diff.lua] false true} 2024/03/23 15:14:30 Plugin does not exist: ftoptions at ftoptions : &{ftoptions ftoptions [runtime/plugins/ftoptions/ftoptions.lua] false true} ... These errors are caused simply by the fact that plugins are initialized but not loaded. Adding config.LoadAllPlugins() to buffer_test.go "fixes" this problem. However, at the moment it doesn't seem a good idea to load plugins in buffer_test.go, since buffer_test.go doesn't properly initialize Lua. It only does ulua.L = lua.NewState() but doesn't do the other stuff that init() in cmd/micro/initlua.go does. As a result, plugins will not be able to do anything correctly. So in order to initialize Lua correctly we need to be inside cmd/micro/, so we cannot do it in buffer_test.go or any other tests except micro_test.go. --- internal/buffer/buffer_test.go | 1 - internal/config/rtfiles_test.go | 1 - 2 files changed, 2 deletions(-) diff --git a/internal/buffer/buffer_test.go b/internal/buffer/buffer_test.go index 167144264..8bb7da402 100644 --- a/internal/buffer/buffer_test.go +++ b/internal/buffer/buffer_test.go @@ -21,7 +21,6 @@ type operation struct { func init() { ulua.L = lua.NewState() config.InitRuntimeFiles() - config.InitPlugins() config.InitGlobalSettings() config.GlobalSettings["backup"] = false config.GlobalSettings["fastdirty"] = true diff --git a/internal/config/rtfiles_test.go b/internal/config/rtfiles_test.go index 694e4686e..de6525ef2 100644 --- a/internal/config/rtfiles_test.go +++ b/internal/config/rtfiles_test.go @@ -8,7 +8,6 @@ import ( func init() { InitRuntimeFiles() - InitPlugins() } func TestAddFile(t *testing.T) { From baca0e5cb2e3ce44b74c5fab03b7c49b464cf57f Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Wed, 3 Apr 2024 03:41:06 +0200 Subject: [PATCH 2/4] Add param to InitRuntimeFiles() to init built-in files only --- cmd/micro/micro.go | 2 +- cmd/micro/micro_test.go | 2 +- internal/action/command.go | 2 +- internal/buffer/buffer_test.go | 2 +- internal/buffer/settings.go | 2 +- internal/config/rtfiles.go | 10 +++++++--- internal/config/rtfiles_test.go | 2 +- 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 225dfc850..f45b7c120 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -254,7 +254,7 @@ func main() { screen.TermMessage(err) } - config.InitRuntimeFiles() + config.InitRuntimeFiles(true) config.InitPlugins() err = config.ReadSettings() diff --git a/cmd/micro/micro_test.go b/cmd/micro/micro_test.go index 7ee521cb7..4c5ca6846 100644 --- a/cmd/micro/micro_test.go +++ b/cmd/micro/micro_test.go @@ -35,7 +35,7 @@ func startup(args []string) (tcell.SimulationScreen, error) { return nil, err } - config.InitRuntimeFiles() + config.InitRuntimeFiles(true) config.InitPlugins() err = config.ReadSettings() diff --git a/internal/action/command.go b/internal/action/command.go index e30a4841e..cd0afecee 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -348,7 +348,7 @@ func reloadRuntime(reloadPlugins bool) { } } - config.InitRuntimeFiles() + config.InitRuntimeFiles(true) if reloadPlugins { config.InitPlugins() diff --git a/internal/buffer/buffer_test.go b/internal/buffer/buffer_test.go index 8bb7da402..ce1647298 100644 --- a/internal/buffer/buffer_test.go +++ b/internal/buffer/buffer_test.go @@ -20,7 +20,7 @@ type operation struct { func init() { ulua.L = lua.NewState() - config.InitRuntimeFiles() + config.InitRuntimeFiles(true) config.InitGlobalSettings() config.GlobalSettings["backup"] = false config.GlobalSettings["fastdirty"] = true diff --git a/internal/buffer/settings.go b/internal/buffer/settings.go index 52cbafdb6..aa011240e 100644 --- a/internal/buffer/settings.go +++ b/internal/buffer/settings.go @@ -20,7 +20,7 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error { } else if option == "statusline" { screen.Redraw() } else if option == "filetype" { - config.InitRuntimeFiles() + config.InitRuntimeFiles(true) err := config.ReadSettings() if err != nil { screen.TermMessage(err) diff --git a/internal/config/rtfiles.go b/internal/config/rtfiles.go index 53820c5f7..015a09b89 100644 --- a/internal/config/rtfiles.go +++ b/internal/config/rtfiles.go @@ -166,10 +166,14 @@ func ListRealRuntimeFiles(fileType RTFiletype) []RuntimeFile { return realFiles[fileType] } -// InitRuntimeFiles initializes all assets file and the config directory -func InitRuntimeFiles() { +// InitRuntimeFiles initializes all assets files and the config directory. +// If `user` is false, InitRuntimeFiles ignores the config directory and +// initializes asset files only. +func InitRuntimeFiles(user bool) { add := func(fileType RTFiletype, dir, pattern string) { - AddRuntimeFilesFromDirectory(fileType, filepath.Join(ConfigDir, dir), pattern) + if user { + AddRuntimeFilesFromDirectory(fileType, filepath.Join(ConfigDir, dir), pattern) + } AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern) } diff --git a/internal/config/rtfiles_test.go b/internal/config/rtfiles_test.go index de6525ef2..8b3f23d2a 100644 --- a/internal/config/rtfiles_test.go +++ b/internal/config/rtfiles_test.go @@ -7,7 +7,7 @@ import ( ) func init() { - InitRuntimeFiles() + InitRuntimeFiles(true) } func TestAddFile(t *testing.T) { From c5d32f625bd26ce2d418fe233e4b178614768bf9 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Wed, 3 Apr 2024 03:44:15 +0200 Subject: [PATCH 3/4] Ignore user-defined runtime files in buffer test and rtfiles test When initializing runtime files (syntax files etc) in tests, initialize built-in runtime files only, to ensure that the tests are not affected by whatever is in ~/.config/micro/ on the test machine. micro_test.go already ensures that, by using its own temporary directory as an (empty) config directory. So we only need to fix buffer_test.go and rtfiles_test.go. In those tests, don't repeat the same dance with a temporary directory, instead just ignore the config directory. --- internal/buffer/buffer_test.go | 2 +- internal/config/rtfiles_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/buffer/buffer_test.go b/internal/buffer/buffer_test.go index ce1647298..7d7602e44 100644 --- a/internal/buffer/buffer_test.go +++ b/internal/buffer/buffer_test.go @@ -20,7 +20,7 @@ type operation struct { func init() { ulua.L = lua.NewState() - config.InitRuntimeFiles(true) + config.InitRuntimeFiles(false) config.InitGlobalSettings() config.GlobalSettings["backup"] = false config.GlobalSettings["fastdirty"] = true diff --git a/internal/config/rtfiles_test.go b/internal/config/rtfiles_test.go index 8b3f23d2a..38c8e9650 100644 --- a/internal/config/rtfiles_test.go +++ b/internal/config/rtfiles_test.go @@ -7,7 +7,7 @@ import ( ) func init() { - InitRuntimeFiles(true) + InitRuntimeFiles(false) } func TestAddFile(t *testing.T) { From 69dc54b407d14101e1ca6f8caafc44b10e074981 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Wed, 3 Apr 2024 04:37:44 +0200 Subject: [PATCH 4/4] Temporarily don't initialize runtime files in buffer test Adding InitRuntimeFiles() to buffer_test.go has changed the behavior of this test: now it tests not just buffer editing per se, but also how well buffer editing works together with syntax highlighting (since InitRuntimeFiles() loads syntax files, and many of the test buffers match the json header pattern in the json.yaml syntax file, so they are "highlighted" as json). This revealed long existing races between buffer editing and syntax highlighting. Until we fix those races, temporarily disable InitRuntimeFiles() in this test. --- internal/buffer/buffer_test.go | 4 +++- internal/config/rtfiles.go | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/buffer/buffer_test.go b/internal/buffer/buffer_test.go index 7d7602e44..dcfde6105 100644 --- a/internal/buffer/buffer_test.go +++ b/internal/buffer/buffer_test.go @@ -20,7 +20,9 @@ type operation struct { func init() { ulua.L = lua.NewState() - config.InitRuntimeFiles(false) + // TODO: uncomment InitRuntimeFiles once we fix races between syntax + // highlighting and buffer editing. + // config.InitRuntimeFiles(false) config.InitGlobalSettings() config.GlobalSettings["backup"] = false config.GlobalSettings["fastdirty"] = true diff --git a/internal/config/rtfiles.go b/internal/config/rtfiles.go index 015a09b89..24adaebfa 100644 --- a/internal/config/rtfiles.go +++ b/internal/config/rtfiles.go @@ -39,6 +39,10 @@ type RuntimeFile interface { var allFiles [][]RuntimeFile var realFiles [][]RuntimeFile +func init() { + initRuntimeVars() +} + func initRuntimeVars() { allFiles = make([][]RuntimeFile, NumTypes) realFiles = make([][]RuntimeFile, NumTypes)