diff options
| author | Shulhan <ms@kilabit.info> | 2026-02-03 17:22:00 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-02-03 17:22:23 +0700 |
| commit | 128497e265bab37db8f2b1385f216342968c9854 (patch) | |
| tree | 8c09b8e6cc843699988cbc370729e0a833fa99ad | |
| parent | ca3002fb7041a863600042f013e12faf73caf227 (diff) | |
| download | pakakeh.go-128497e265bab37db8f2b1385f216342968c9854.tar.xz | |
lib/os: fix IsBinary that return true if file size less than 1024
If file size less than half of 1024, the rest of bytes will be `0` and it
will counted as binary character.
| -rw-r--r-- | lib/os/os.go | 4 | ||||
| -rw-r--r-- | lib/os/os_test.go | 23 | ||||
| -rw-r--r-- | lib/os/testdata/IsBinary/awwan.conf | 11 |
3 files changed, 37 insertions, 1 deletions
diff --git a/lib/os/os.go b/lib/os/os.go index 24946792..c5edad86 100644 --- a/lib/os/os.go +++ b/lib/os/os.go @@ -160,7 +160,7 @@ func IsBinary(file string) (is bool) { is = true content := make([]byte, 1024) - _, err = f.Read(content) + n, err := f.Read(content) if err != nil { is = false } @@ -169,8 +169,10 @@ func IsBinary(file string) (is bool) { is = false } if !is { + // There is an error during Read and Close. return is } + content = content[:n] return IsBinaryStream(content) } diff --git a/lib/os/os_test.go b/lib/os/os_test.go index 88c420c5..78d31b36 100644 --- a/lib/os/os_test.go +++ b/lib/os/os_test.go @@ -115,6 +115,26 @@ func TestCopy(t *testing.T) { } } +func TestIsBinary(t *testing.T) { + listCase := []struct { + path string + exp bool + }{{ + path: `testdata/exp.bz2`, + exp: true, + }, { + path: `os.go`, + exp: false, + }, { + path: `testdata/IsBinary/awwan.conf`, + exp: false, + }} + for _, tc := range listCase { + got := IsBinary(tc.path) + test.Assert(t, tc.path, tc.exp, got) + } +} + func TestIsBinaryStream(t *testing.T) { listCase := []struct { path string @@ -125,6 +145,9 @@ func TestIsBinaryStream(t *testing.T) { }, { path: `os.go`, exp: false, + }, { + path: `testdata/IsBinary/awwan.conf`, + exp: false, }} for _, tc := range listCase { content, err := os.ReadFile(tc.path) diff --git a/lib/os/testdata/IsBinary/awwan.conf b/lib/os/testdata/IsBinary/awwan.conf new file mode 100644 index 00000000..d34403de --- /dev/null +++ b/lib/os/testdata/IsBinary/awwan.conf @@ -0,0 +1,11 @@ +[job "webhook_awwan"] +description = Webhook for \ + <a href="https://kilabit.info/project/awwan">awwan</a>. +auth_kind = sourcehut +secret = {{.Val "karajo:secret:webhook"}} +path = /webhook/awwan +notif_on_failed = email + +command = /var/lib/karajo/git-clone-fetch.sh \ + git@git.sr.ht:~shulhan/awwan origin/main _AUR/change.log +command = cd _AUR && /var/lib/karajo/aur-build.sh |
