Skip to content

Commit

Permalink
test: don't use real fixture file
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Jun 17, 2024
1 parent 33458bb commit 12e611d
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 22 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ jobs:
- uses: taiki-e/install-action@just
- uses: taiki-e/install-action@nextest

- name: Prepare config
run: |
mkdir -p tests/.config/bilal
cp examples/config.toml tests/.config/bilal
- name: Run tests
run: just test

Expand Down
127 changes: 127 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ toml = { version = "0.8", features = ["parse"] }

[dev-dependencies]
assert_cmd = "2.0"
assert_fs = "1.1.1"
predicates = "3.1"

[package.metadata.binstall]
Expand Down
26 changes: 15 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ fn parse(content: &str) -> Result<Config, Error> {

/// Return configuration path
fn path() -> Result<PathBuf, Error> {
let path = if cfg!(windows) {
Path::new(&std::env::var("APPDATA")?)
.join("Bilal")
.join("config.toml")
} else {
Path::new(&std::env::var("HOME")?)
.join(".config")
.join("bilal")
.join("config.toml")
};
Ok(path)
match &std::env::var("BILAL_CONFIG") {
Err(_) => {
if cfg!(windows) {
Ok(Path::new(&std::env::var("APPDATA")?)
.join("Bilal")
.join("config.toml"))
} else {
Ok(Path::new(&std::env::var("HOME")?)
.join(".config")
.join("bilal")
.join("config.toml"))
}
}
Ok(path) => Ok(PathBuf::from(path)),
}
}

fn deserialize_time_format<'de, D>(deserializer: D) -> Result<TimeFormat, D::Error>
Expand Down
39 changes: 33 additions & 6 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{env, error::Error, process::Command};

use assert_cmd::{crate_name, prelude::*};
use assert_fs::{prelude::*, TempDir};
use predicates::prelude::*;

#[test]
Expand All @@ -15,8 +16,11 @@ fn help() -> Result<(), Box<dyn Error>> {

#[test]
fn all() -> Result<(), Box<dyn Error>> {
env::set_var("HOME", "./tests");
env::set_var("APPDATA", "./tests");
let temp_dir = setup_config()?;
env::set_var(
"BILAL_CONFIG",
format!("{}/config.toml", temp_dir.path().display()),
);

let mut cmd = Command::cargo_bin(crate_name!())?;
cmd.arg("all");
Expand All @@ -28,8 +32,11 @@ fn all() -> Result<(), Box<dyn Error>> {

#[test]
fn current() -> Result<(), Box<dyn Error>> {
env::set_var("HOME", "./tests");
env::set_var("APPDATA", "./tests");
let temp_dir = setup_config()?;
env::set_var(
"BILAL_CONFIG",
format!("{}/config.toml", temp_dir.path().display()),
);

let mut cmd = Command::cargo_bin(crate_name!())?;
cmd.arg("current").arg("--json");
Expand All @@ -42,8 +49,11 @@ fn current() -> Result<(), Box<dyn Error>> {

#[test]
fn next() -> Result<(), Box<dyn Error>> {
env::set_var("HOME", "./tests");
env::set_var("APPDATA", "./tests");
let temp_dir = setup_config()?;
env::set_var(
"BILAL_CONFIG",
format!("{}/config.toml", temp_dir.path().display()),
);

let mut cmd = Command::cargo_bin(crate_name!())?;
cmd.arg("next").arg("--json");
Expand All @@ -53,3 +63,20 @@ fn next() -> Result<(), Box<dyn Error>> {
.stdout(predicate::str::contains("\u{25b6}"));
Ok(())
}

fn setup_config() -> Result<TempDir, Box<dyn Error>> {
let temp_dir = assert_fs::TempDir::new()?;
let config = temp_dir.child("config.toml");
config.write_str(&config_base())?;
Ok(temp_dir)
}

fn config_base() -> String {
let content = r#"
latitude = -6.18233995
longitude = 106.84287154
madhab = "Shafi"
method = "Egyptian"
"#;
content.to_string()
}

0 comments on commit 12e611d

Please sign in to comment.