From 923bcd2f8c05b4da5e5dc18bc1aca0a5d25fad17 Mon Sep 17 00:00:00 2001 From: soulteary Date: Wed, 15 Jun 2022 10:55:10 +0800 Subject: [PATCH] chore: improve coverage --- README.md | 15 ++++++++------- state/custom_test.go | 27 +++++++++++++++++++++++++++ state/state.go | 26 ++++++++++++++++---------- 3 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 state/custom_test.go diff --git a/README.md b/README.md index 04e9d4a..be7568b 100644 --- a/README.md +++ b/README.md @@ -121,14 +121,15 @@ go run apt-proxy.go # go test -cover ./... ? github.com/soulteary/apt-proxy [no test files] -ok github.com/soulteary/apt-proxy/cli 3.889s coverage: 73.9% of statements -ok github.com/soulteary/apt-proxy/linux 9.660s coverage: 80.0% of statements -ok github.com/soulteary/apt-proxy/pkgs/httpcache 1.943s coverage: 82.7% of statements +ok github.com/soulteary/apt-proxy/cli 2.477s coverage: 73.9% of statements +ok github.com/soulteary/apt-proxy/linux 7.329s coverage: 80.0% of statements +ok github.com/soulteary/apt-proxy/pkgs/httpcache 1.062s coverage: 82.7% of statements ? github.com/soulteary/apt-proxy/pkgs/httplog [no test files] -ok github.com/soulteary/apt-proxy/pkgs/stream.v1 0.934s coverage: 100.0% of statements -ok github.com/soulteary/apt-proxy/pkgs/vfs 1.069s coverage: 59.3% of statements -? github.com/soulteary/apt-proxy/proxy [no test files] -ok github.com/soulteary/apt-proxy/state 1.555s coverage: 100.0% of statements +ok github.com/soulteary/apt-proxy/pkgs/stream.v1 0.551s coverage: 100.0% of statements +? github.com/soulteary/apt-proxy/pkgs/system [no test files] +ok github.com/soulteary/apt-proxy/pkgs/vfs 0.719s coverage: 59.3% of statements +? github.com/soulteary/apt-proxy/server [no test files] +ok github.com/soulteary/apt-proxy/state 0.617s coverage: 84.2% of statements ``` View coverage report: diff --git a/state/custom_test.go b/state/custom_test.go new file mode 100644 index 0000000..c3e9359 --- /dev/null +++ b/state/custom_test.go @@ -0,0 +1,27 @@ +package state + +import "testing" + +func TestGetUbuntuMirrorByAliases(t *testing.T) { + alias := getUbuntuMirrorByAliases("cn:tsinghua") + if alias != BUILDIN_CUSTOM_UBUNTU_MIRRORS[0].url { + t.Fatal("Test Get Mirror By Custom Name Failed") + } + + alias = getUbuntuMirrorByAliases("cn:not-found") + if alias != "" { + t.Fatal("Test Get Mirror By Custom Name Failed") + } +} + +func TestGetDebianMirrorByAliases(t *testing.T) { + alias := getDebianMirrorByAliases("cn:tsinghua") + if alias != BUILDIN_CUSTOM_DEBIAN_MIRRORS[0].url { + t.Fatal("Test Get Mirror By Custom Name Failed") + } + + alias = getDebianMirrorByAliases("cn:not-found") + if alias != "" { + t.Fatal("Test Get Mirror By Custom Name Failed") + } +} diff --git a/state/state.go b/state/state.go index 18360d2..57ffc35 100644 --- a/state/state.go +++ b/state/state.go @@ -17,16 +17,19 @@ func GetProxyMode() int { } func SetUbuntuMirror(input string) { - mirror := "" + if input == "" { + UBUNTU_MIRROR = nil + return + } + + mirror := input alias := getUbuntuMirrorByAliases(input) - if alias == "" { - mirror = input - } else { + if alias != "" { mirror = alias } url, err := url.Parse(mirror) - if err != nil || mirror == "" { + if err != nil { UBUNTU_MIRROR = nil return } @@ -42,16 +45,19 @@ func ResetUbuntuMirror() { } func SetDebianMirror(input string) { - mirror := "" + if input == "" { + DEBIAN_MIRROR = nil + return + } + + mirror := input alias := getDebianMirrorByAliases(input) - if alias == "" { - mirror = input - } else { + if alias != "" { mirror = alias } url, err := url.Parse(mirror) - if err != nil || mirror == "" { + if err != nil { DEBIAN_MIRROR = nil return }