From de838ce7e4f6ca44857ed2ca8aefaa42d0dd0f7c Mon Sep 17 00:00:00 2001 From: thinkgo Date: Wed, 13 Dec 2023 14:02:34 +0800 Subject: [PATCH] fix: add more test --- Cargo.lock | 51 ++++++++++++++++++++++++++++++++++++++ goup-consts/Cargo.toml | 3 +++ goup-consts/src/consts.rs | 52 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c600d1a..f12be78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -732,6 +732,9 @@ dependencies = [ [[package]] name = "goup-consts" version = "0.2.5" +dependencies = [ + "temp-env", +] [[package]] name = "goup-downloader" @@ -1080,6 +1083,16 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" +[[package]] +name = "lock_api" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.20" @@ -1170,6 +1183,29 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.4.1", + "smallvec", + "windows-targets 0.48.5", +] + [[package]] name = "password-hash" version = "0.4.2" @@ -1687,6 +1723,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" + [[package]] name = "socket2" version = "0.4.9" @@ -1778,6 +1820,15 @@ dependencies = [ "xattr", ] +[[package]] +name = "temp-env" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96374855068f47402c3121c6eed88d29cb1de8f3ab27090e273e420bdabcf050" +dependencies = [ + "parking_lot", +] + [[package]] name = "tempfile" version = "3.8.1" diff --git a/goup-consts/Cargo.toml b/goup-consts/Cargo.toml index 20b30b7..ada49a3 100644 --- a/goup-consts/Cargo.toml +++ b/goup-consts/Cargo.toml @@ -20,3 +20,6 @@ repository = "https://github.com/thinkgos/goup-rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] + +[dev-dependencies] +temp-env = "0.3" \ No newline at end of file diff --git a/goup-consts/src/consts.rs b/goup-consts/src/consts.rs index 9dc8ed0..cd51bb4 100644 --- a/goup-consts/src/consts.rs +++ b/goup-consts/src/consts.rs @@ -49,9 +49,61 @@ pub fn go_version_archive_url(version: &str) -> String { ) } +#[inline] fn get_var_or_else(key: &str, op: impl FnOnce() -> String) -> String { env::var(key) .ok() .filter(|s| !s.is_empty()) .unwrap_or_else(op) } + +#[cfg(test)] +mod tests { + use super::{ + go_download_base_url, go_host, go_source_git_url, go_source_upstream_git_url, + go_version_archive_url, + }; + use super::{GOUP_GO_DOWNLOAD_BASE_URL, GOUP_GO_HOST, GOUP_GO_SOURCE_GIT_URL}; + use super::{GO_DOWNLOAD_BASE_URL, GO_HOST, GO_SOURCE_GIT_URL, GO_SOURCE_UPSTREAM_GIT_URL}; + + #[test] + fn test_env_vars_unset() { + temp_env::with_vars_unset( + [ + GOUP_GO_HOST, + GOUP_GO_DOWNLOAD_BASE_URL, + GOUP_GO_SOURCE_GIT_URL, + ], + || { + assert_eq!(go_host(), GO_HOST); + assert_eq!(go_download_base_url(), GO_DOWNLOAD_BASE_URL); + assert_eq!(go_source_git_url(), GO_SOURCE_GIT_URL); + assert_eq!(go_source_upstream_git_url(), GO_SOURCE_UPSTREAM_GIT_URL); + }, + ) + } + #[test] + fn test_env_vars_set() { + let test_go_host = "https://golang.google.cn"; + let test_go_download_base_url = "https://golang.google.cn/dl"; + let test_go_source_git_url = "https://go.googlesource.com/go"; + temp_env::with_vars( + [ + (GOUP_GO_HOST, Some(test_go_host)), + (GOUP_GO_DOWNLOAD_BASE_URL, Some(test_go_download_base_url)), + (GOUP_GO_SOURCE_GIT_URL, Some(test_go_source_git_url)), + ], + || { + assert_eq!(go_host(), test_go_host); + assert_eq!(go_download_base_url(), test_go_download_base_url); + assert_eq!(go_source_git_url(), test_go_source_git_url); + assert_eq!(go_source_upstream_git_url(), test_go_source_git_url); + }, + ) + } + + #[test] + fn test_archive_url() { + assert!(go_version_archive_url("1.21.5").starts_with("https://dl.google.com/go/1.21.5")) + } +}