From 8d47b6c50873f31fe7ac0865e3c681b9fabeca7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20M=C3=B6ller?= Date: Mon, 5 Aug 2024 23:27:25 +0200 Subject: [PATCH] fix: also change comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakob Möller --- config.go | 8 ++++---- config_test.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index 7c9fc45..bcb4947 100644 --- a/config.go +++ b/config.go @@ -305,6 +305,7 @@ func updateConfig(ctx context.Context, v any, rw io.ReadWriteSeeker) error { fieldRegexes := make([]fieldRegex, 0, len(fieldsForConfigQuery)) for _, field := range fieldsForConfigQuery { // The pattern is a regex that matches the field name and its value if uncommented + // It also matches the comment that indicates the field was edited by the client. // Valid: // field = value // field = "value" @@ -312,7 +313,7 @@ func updateConfig(ctx context.Context, v any, rw io.ReadWriteSeeker) error { // # field = value // # field = "value" // It incorporates various tabbing and spacing configurations as well - pattern := fmt.Sprintf(`(?m)^\s*%s\s*=\s*(\".*?\"|\d+)?$`, field.name) + pattern := fmt.Sprintf(`(?m)((\t# .*?\n)*|)^\s%s\s*=\s*(\".*?\"|\d+)?$`, field.name) re, err := regexp.Compile(pattern) if err != nil { return fmt.Errorf("failed to compile regexp: %v", err) @@ -354,7 +355,7 @@ func updateConfig(ctx context.Context, v any, rw io.ReadWriteSeeker) error { if diff := len(raw) - offset; diff < 0 { // If the old configuration is smaller than the new configuration, we need to append the difference // with empty bytes to ensure we do not have leftover data from the old configuration - raw = append(raw, make([]byte, diff)...) + raw = append(raw, make([]byte, -diff)...) } // We want to write from the start, so seek back to the start of the configuration @@ -369,8 +370,7 @@ func updateConfig(ctx context.Context, v any, rw io.ReadWriteSeeker) error { // generateLVMConfigEditComment generates a comment to be added to the configuration file // This comment is used to indicate that the field was edited by the client. func generateLVMConfigEditComment() string { - return fmt.Sprintf(` - # This field was edited by %s at %s + return fmt.Sprintf(` # This field was edited by %s at %s # Proceed carefully when editing as it can have unintended consequences with code relying on this field. `, ModuleID(), time.Now().Format(time.RFC3339)) } diff --git a/config_test.go b/config_test.go index c29902f..3ffc6b6 100644 --- a/config_test.go +++ b/config_test.go @@ -313,7 +313,7 @@ func TestUpdateGlobalConfig(t *testing.T) { } `lvm:"config"` }{} - expectedPreamble := "\n\t# Proceed carefully when editing as it can have unintended consequences with code relying on this field.\n\t" + expectedPreamble := "# Proceed carefully when editing as it can have unintended consequences with code relying on this field.\n\t" cfg.Config.ProfileDir = "mynewprofiledir" @@ -340,6 +340,33 @@ func TestUpdateGlobalConfig(t *testing.T) { t.Fatalf("expected field to be modified, but it was not") } - t.Log(LVMGlobalConfiguration) + cfg.Config.ProfileDir = "mynewprofiledir2" + + if err := clnt.UpdateGlobalConfig(context.Background(), &cfg); err != nil { + t.Fatalf("failed to update global config: %v", err) + } + println(control().String()) + + if containsModifiedField = bytes.Contains(control().Bytes(), []byte(fmt.Sprintf( + "%sprofile_dir = %q\n", + expectedPreamble, + "mynewprofiledir2", + ))); !containsModifiedField { + t.Fatalf("expected field to be modified, but it was not") + } + + cfg.Config.ProfileDir = "mynewprofiledir3" + + if err := clnt.UpdateGlobalConfig(context.Background(), &cfg); err != nil { + t.Fatalf("failed to update global config: %v", err) + } + + if containsModifiedField = bytes.Contains(control().Bytes(), []byte(fmt.Sprintf( + "%sprofile_dir = %q\n", + expectedPreamble, + "mynewprofiledir3", + ))); !containsModifiedField { + t.Fatalf("expected field to be modified, but it was not") + } }