Skip to content

Commit

Permalink
test: test utils (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
shixinhuang99 committed Jan 20, 2024
1 parent 637753d commit 0fd29a7
Show file tree
Hide file tree
Showing 7 changed files with 653 additions and 421 deletions.
74 changes: 48 additions & 26 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,57 +44,79 @@ impl Config {
}

#[cfg(test)]
mod tests {
mod test_utils {
use std::fs;

use anyhow::Result;
use tempfile::{tempdir, TempDir};

use super::Config;

fn mock_config(create_file: bool) -> Result<(Config, TempDir)> {
let temp_dir = tempdir()?;
let temp_dir_path = temp_dir.path();
pub struct ConfigMock {
pub config: Config,
pub tmpdir: TempDir,
}

impl ConfigMock {
pub fn new() -> Self {
let tmpdir = tempdir().unwrap();
let config = Config::new(tmpdir.path()).unwrap();

Self {
tmpdir,
config,
}
}

pub fn with_content(self) -> Self {
let tmpdir_path = self.tmpdir.path();

if create_file {
fs::write(
temp_dir_path.join(Config::FILE_NAME),
tmpdir_path.join(Config::FILE_NAME),
"{\n \"token\": \"token\"\n}",
)?;
}
)
.unwrap();

let config = Config::new(temp_dir_path)?;
let config = Config::new(tmpdir_path).unwrap();

Ok((config, temp_dir))
Self {
config,
..self
}
}
}
}

#[test]
fn test_config_new_not_exists() -> Result<()> {
let (config, _dir) = mock_config(false)?;
#[cfg(test)]
mod tests {
use std::fs;

assert_eq!(config.token(), None);
use anyhow::Result;

Ok(())
}
use super::test_utils::ConfigMock;

#[test]
fn test_config_new_exists() -> Result<()> {
let (config, _dir) = mock_config(true)?;
fn test_config_new_not_exists() {
let config_mock = ConfigMock::new();

assert_eq!(config.token(), Some("token"));
assert_eq!(config_mock.config.token(), None);
}

Ok(())
#[test]
fn test_config_new_exists() {
let config_mock = ConfigMock::new().with_content();

assert_eq!(config_mock.config.token(), Some("token"));
}

#[test]
fn test_config_save() -> Result<()> {
let (mut config, _dir) = mock_config(false)?;
let mut config_mock = ConfigMock::new();

config.set_token("token2");
config.save()?;
config_mock.config.set_token("token2");
config_mock.config.save()?;

let content = fs::read_to_string(&config.path)?;
assert_eq!(content, "{\n \"token\": \"token2\"\n}");
let actual = fs::read_to_string(&config_mock.config.path)?;
assert_eq!(actual, "{\n \"token\": \"token2\"\n}");

Ok(())
}
Expand Down
144 changes: 93 additions & 51 deletions src/github_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,38 +107,97 @@ impl GitHubApi {
}

#[cfg(test)]
mod tests {
use anyhow::Result;
mod test_utils {
use mockito::{Mock, ServerGuard};

use super::GitHubApi;
use crate::repository::Repository;

fn mock_repo() -> Repository {
Repository {
owner: "shixinhuang99".to_string(),
name: "scafalra".to_string(),
..Repository::default()
pub struct RepositoryMock {
owner: String,
name: String,
}

impl RepositoryMock {
pub fn new() -> Self {
Self {
owner: "shixinhuang99".to_string(),
name: "scafalra".to_string(),
}
}

pub fn build(self) -> Repository {
Repository {
owner: self.owner,
name: self.name,
..Repository::default()
}
}

pub fn owner(self, owner: &str) -> Self {
Self {
owner: owner.to_string(),
..self
}
}

pub fn name(self, name: &str) -> Self {
Self {
name: name.to_string(),
..self
}
}
}

#[test]
fn test_repo_query() -> Result<()> {
let mut server = mockito::Server::new();
pub struct GitHubApiMock {
pub github_api: GitHubApi,
pub server: ServerGuard,
pub mock: Mock,
}

let mock = server
.mock("POST", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_file("fixtures/repo-query-response.json")
.create();
impl GitHubApiMock {
pub fn new(fixture: &str) -> Self {
let mut server = mockito::Server::new();

let github_api = GitHubApi::new(Some(&server.url()));
let mock = server
.mock("POST", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_file(fixture)
.create();

github_api.set_token("token");
let github_api = GitHubApi::new(Some(&server.url()));

let repo_urls = github_api.query_remote_repo(&mock_repo())?;
Self {
github_api,
server,
mock,
}
}
}
}

mock.assert();
#[cfg(test)]
mod tests {
use anyhow::Result;

use super::{
test_utils::{GitHubApiMock, RepositoryMock},
GitHubApi,
};

#[test]
fn test_repo_query() -> Result<()> {
let github_api_mock =
GitHubApiMock::new("fixtures/repo-query-response.json");

github_api_mock.github_api.set_token("token");

let repo_urls = github_api_mock
.github_api
.query_remote_repo(&RepositoryMock::new().build())?;

github_api_mock.mock.assert();
assert_eq!(repo_urls.url, "url");
assert_eq!(repo_urls.tarball_url, "tarballUrl");

Expand All @@ -148,33 +207,24 @@ mod tests {
#[test]
fn test_github_api_request_no_token() {
let github_api = GitHubApi::new(None);
let api_result = github_api.query_remote_repo(&mock_repo());
let api_result =
github_api.query_remote_repo(&RepositoryMock::new().build());

assert!(api_result.is_err());
}

#[test]
fn test_github_api_request_error() -> Result<()> {
let mut server = mockito::Server::new();
let github_api_mock =
GitHubApiMock::new("fixtures/repo-query-error.json");

let mock = server
.mock("POST", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_file("fixtures/repo-query-error.json")
.create();
github_api_mock.github_api.set_token("token");

let github_api = GitHubApi::new(Some(&server.url()));
let api_result = github_api_mock.github_api.query_remote_repo(
&RepositoryMock::new().owner("foo").name("bar").build(),
);

github_api.set_token("token");

let api_result = github_api.query_remote_repo(&Repository {
owner: "foo".to_string(),
name: "bar".to_string(),
..Repository::default()
});

mock.assert();
github_api_mock.mock.assert();
assert!(api_result.is_err());

Ok(())
Expand All @@ -183,22 +233,14 @@ mod tests {
#[test]
#[cfg(feature = "self_update")]
fn test_release_query() -> Result<()> {
let mut server = mockito::Server::new();

let mock = server
.mock("POST", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body_from_file("fixtures/release-query-response.json")
.create();

let github_api = GitHubApi::new(Some(&server.url()));
let github_api_mock =
GitHubApiMock::new("fixtures/release-query-response.json");

github_api.set_token("token");
github_api_mock.github_api.set_token("token");

let release = github_api.query_release()?;
let release = github_api_mock.github_api.query_release()?;

mock.assert();
github_api_mock.mock.assert();
assert_eq!(release.version.to_string(), "0.6.0");

Ok(())
Expand Down

0 comments on commit 0fd29a7

Please sign in to comment.