From 42442e8a4995695537b5f69088f7dfca1a179c06 Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Mon, 18 Dec 2023 14:11:30 +0100 Subject: [PATCH] tests: Add removal test when there is no crc section While the removal code when there is no crc section does not seem to be impacted by the issues recently fixed, it does not hurt to have a test for it. --- pkg/hosts/hosts_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/hosts/hosts_test.go b/pkg/hosts/hosts_test.go index 03620f00..1fbc54c1 100644 --- a/pkg/hosts/hosts_test.go +++ b/pkg/hosts/hosts_test.go @@ -282,6 +282,22 @@ func TestRemoveMultipleLines(t *testing.T) { assert.Equal(t, hostsTemplate+eol()+crcSection(), string(content)) } +func TestRemoveMultipleNoCrcSection(t *testing.T) { + dir, err := os.MkdirTemp("", "hosts") + assert.NoError(t, err) + defer os.RemoveAll(dir) + + hostsFile := filepath.Join(dir, "hosts") + assert.NoError(t, os.WriteFile(hostsFile, []byte("192.168.130.11 entry1 entry2"+eol()+"192.168.130.11 entry3 entry4"), 0600)) + host := hosts(t, hostsFile) + + assert.NoError(t, host.Remove([]string{"entry1", "entry2", "entry3", "entry4"})) + + content, err := os.ReadFile(hostsFile) + assert.NoError(t, err) + assert.Equal(t, "", string(content)) +} + func hosts(t *testing.T, hostsFile string) Hosts { config, _ := libhosty.NewHostsFileConfig(hostsFile) file, err := libhosty.InitWithConfig(config)