Create a TestConfig struct to separate setup
I was about to copy-paste the entire body of the attach_file_exists test function into the attach_file_missing function. The only difference is the file that they upload -- or don't, in the second case. I could make a try-file-upload function and pass it many different files, but I don't think I need that. Instead, I'll separate the test setup from the test sequence itself.
This commit is contained in:
@@ -50,47 +50,23 @@ mod tests {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn attach_file_exists() {
|
async fn attach_file_exists() {
|
||||||
let server = std::env::var("TEST_GITEA_SERVER")
|
let conf = TestConfig::new();
|
||||||
.expect("Must set server address in env var \"TEST_GITEA_SERVER\"");
|
|
||||||
let repo = std::env::var("TEST_GITEA_REPO")
|
|
||||||
.expect("Must set <user>/<repo> name in env var \"TEST_GITEA_REPO\"");
|
|
||||||
let token = format!(
|
|
||||||
"token {}",
|
|
||||||
std::env::var("TEST_GITEA_KEY")
|
|
||||||
.expect("Must set the API token in env var \"TEST_GITEA_KEY\"")
|
|
||||||
);
|
|
||||||
let release_tag = std::env::var("TEST_GITEA_RELEASE_TAG")
|
|
||||||
.expect("Must set the target release tag in env var \"TEST_GITEA_RELEASE_TAG\"");
|
|
||||||
|
|
||||||
let mut headers = reqwest::header::HeaderMap::new();
|
|
||||||
headers.append(ACCEPT, header::HeaderValue::from_static("application/json"));
|
|
||||||
headers.append("Authorization", token.parse().unwrap());
|
|
||||||
|
|
||||||
let client = reqwest::Client::builder()
|
|
||||||
.user_agent(format!(
|
|
||||||
"gt-tools-autotest-agent{}",
|
|
||||||
env!("CARGO_PKG_VERSION")
|
|
||||||
))
|
|
||||||
.default_headers(headers)
|
|
||||||
.build()
|
|
||||||
.expect("Failed to build reqwest::Client.");
|
|
||||||
|
|
||||||
let release_candidates =
|
let release_candidates =
|
||||||
crate::api::release::list_releases(
|
crate::api::release::list_releases(
|
||||||
&client,
|
&conf.client,
|
||||||
&server,
|
&conf.server,
|
||||||
&repo
|
&conf.repo
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to get releases. Pre-conditions unmet, aborting test!");
|
.expect("Failed to get releases. Pre-conditions unmet, aborting test!");
|
||||||
|
|
||||||
let release = match_release_by_tag(&release_tag, release_candidates)
|
let release = match_release_by_tag(&conf.release_tag, release_candidates)
|
||||||
.expect("Failed to select matching release. Pre-conditions unmet, aborting test!");
|
.expect("Failed to select matching release. Pre-conditions unmet, aborting test!");
|
||||||
|
|
||||||
let api_result = super::create_release_attachment(
|
let api_result = super::create_release_attachment(
|
||||||
&client,
|
&conf.client,
|
||||||
&server,
|
&conf.server,
|
||||||
&repo,
|
&conf.repo,
|
||||||
release.id,
|
release.id,
|
||||||
vec![String::from("Cargo.toml")],
|
vec![String::from("Cargo.toml")],
|
||||||
)
|
)
|
||||||
@@ -102,6 +78,49 @@ mod tests {
|
|||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct TestConfig {
|
||||||
|
server: String,
|
||||||
|
repo: String,
|
||||||
|
release_tag: String,
|
||||||
|
client: reqwest::Client,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestConfig {
|
||||||
|
fn new() -> Self {
|
||||||
|
let server = std::env::var("TEST_GITEA_SERVER")
|
||||||
|
.expect("Must set server address in env var \"TEST_GITEA_SERVER\"");
|
||||||
|
let repo = std::env::var("TEST_GITEA_REPO")
|
||||||
|
.expect("Must set <user>/<repo> name in env var \"TEST_GITEA_REPO\"");
|
||||||
|
let token = format!(
|
||||||
|
"token {}",
|
||||||
|
std::env::var("TEST_GITEA_KEY")
|
||||||
|
.expect("Must set the API token in env var \"TEST_GITEA_KEY\"")
|
||||||
|
);
|
||||||
|
let release_tag = std::env::var("TEST_GITEA_RELEASE_TAG")
|
||||||
|
.expect("Must set the target release tag in env var \"TEST_GITEA_RELEASE_TAG\"");
|
||||||
|
|
||||||
|
let mut headers = reqwest::header::HeaderMap::new();
|
||||||
|
headers.append(ACCEPT, header::HeaderValue::from_static("application/json"));
|
||||||
|
headers.append("Authorization", token.parse().unwrap());
|
||||||
|
|
||||||
|
let client = reqwest::Client::builder()
|
||||||
|
.user_agent(format!(
|
||||||
|
"gt-tools-autotest-agent{}",
|
||||||
|
env!("CARGO_PKG_VERSION")
|
||||||
|
))
|
||||||
|
.default_headers(headers)
|
||||||
|
.build()
|
||||||
|
.expect("Failed to build reqwest::Client.");
|
||||||
|
|
||||||
|
return Self {
|
||||||
|
server,
|
||||||
|
repo,
|
||||||
|
release_tag,
|
||||||
|
client
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Testing utils
|
// Testing utils
|
||||||
fn match_release_by_tag(tag: &String, releases: Vec<Release>) -> Option<Release> {
|
fn match_release_by_tag(tag: &String, releases: Vec<Release>) -> Option<Release> {
|
||||||
let mut release: Option<Release> = None;
|
let mut release: Option<Release> = None;
|
||||||
|
|||||||
Reference in New Issue
Block a user