From 0b92dbb8463dee31c005c59f67c2bf754a9eae12 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 22 May 2022 21:29:35 +0700 Subject: all: export and rename the hostsBlock type to Blockd In the Environment, we have a field HostsBlocks that reference an unexported type hostsFile. In order for any package to be able to use this field, we need to export this type. While at it, we rename the type from hostsBlock to Blockd to make it simple and consistent with name. --- blockd_test.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 blockd_test.go (limited to 'blockd_test.go') diff --git a/blockd_test.go b/blockd_test.go new file mode 100644 index 0000000..f1d6abf --- /dev/null +++ b/blockd_test.go @@ -0,0 +1,104 @@ +// SPDX-FileCopyrightText: 2020 M. Shulhan +// SPDX-License-Identifier: GPL-3.0-or-later + +package rescached + +import ( + "os" + "path/filepath" + "testing" + + "github.com/shuLhan/share/lib/test" +) + +func TestBlockd_init(t *testing.T) { + type testCase struct { + desc string + hb Blockd + exp Blockd + } + + var ( + testDirBase = t.TempDir() + pathDirBlock = filepath.Join(testDirBase, dirBlock) + fileEnabled = "fileEnabled" + fileDisabled = "fileDisabled" + fileNotExist = "fileNotExist" + hostsFileEnabled = filepath.Join(pathDirBlock, fileEnabled) + hostsFileDisabled = filepath.Join(pathDirBlock, "."+fileDisabled) + + fiEnabled os.FileInfo + fiDisabled os.FileInfo + cases []testCase + c testCase + err error + ) + + err = os.MkdirAll(pathDirBlock, 0700) + if err != nil { + t.Fatal(err) + } + + err = os.WriteFile(hostsFileEnabled, []byte("127.0.0.2 localhost"), 0600) + if err != nil { + t.Fatal(err) + } + err = os.WriteFile(hostsFileDisabled, []byte("127.0.0.2 localhost"), 0600) + if err != nil { + t.Fatal(err) + } + + fiEnabled, err = os.Stat(hostsFileEnabled) + if err != nil { + t.Fatal(err) + } + fiDisabled, err = os.Stat(hostsFileDisabled) + if err != nil { + t.Fatal(err) + } + + cases = []testCase{{ + desc: "With hosts block file enabled", + hb: Blockd{ + Name: fileEnabled, + }, + exp: Blockd{ + Name: fileEnabled, + lastUpdated: fiEnabled.ModTime(), + LastUpdated: fiEnabled.ModTime().Format(lastUpdatedFormat), + file: hostsFileEnabled, + fileDisabled: filepath.Join(pathDirBlock, "."+fileEnabled), + IsEnabled: true, + isFileExist: true, + }, + }, { + desc: "With hosts block file disabled", + hb: Blockd{ + Name: fileDisabled, + }, + exp: Blockd{ + Name: fileDisabled, + lastUpdated: fiDisabled.ModTime(), + LastUpdated: fiDisabled.ModTime().Format(lastUpdatedFormat), + file: filepath.Join(pathDirBlock, fileDisabled), + fileDisabled: hostsFileDisabled, + isFileExist: true, + }, + }, { + desc: "With hosts block file not exist", + hb: Blockd{ + Name: fileNotExist, + }, + exp: Blockd{ + Name: fileNotExist, + file: filepath.Join(pathDirBlock, fileNotExist), + fileDisabled: filepath.Join(pathDirBlock, "."+fileNotExist), + }, + }} + + for _, c = range cases { + c.hb.init(pathDirBlock) + + test.Assert(t, c.desc, c.exp, c.hb) + } +} -- cgit v1.3