Skip to content

Commit

Permalink
fix: add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Dec 13, 2023
1 parent bae8201 commit de838ce
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions goup-consts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
52 changes: 52 additions & 0 deletions goup-consts/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}

0 comments on commit de838ce

Please sign in to comment.