test(docker_context): Add basic tests to module (#2205)
* test(docker_context): Add basic tests to module This adds some basic tests to the docker_context module. * PR suggestion
This commit is contained in:
parent
e23f0f7605
commit
762ad12698
|
@ -716,8 +716,8 @@ The `docker_context` module shows the currently active
|
||||||
| `format` | `"via [$symbol$context]($style) "` | The format for the module. |
|
| `format` | `"via [$symbol$context]($style) "` | The format for the module. |
|
||||||
| `symbol` | `"🐳 "` | The symbol used before displaying the Docker context. |
|
| `symbol` | `"🐳 "` | The symbol used before displaying the Docker context. |
|
||||||
| `style` | `"blue bold"` | The style for the module. |
|
| `style` | `"blue bold"` | The style for the module. |
|
||||||
| `only_with_files` | `false` | Only show when there's a `docker-compose.yml`, `docker-compose.yaml`, or `Dockerfile` in the current directory. |
|
| `only_with_files` | `true` | Only show when there's a `docker-compose.yml`, `docker-compose.yaml`, or `Dockerfile` in the current directory. |
|
||||||
| `disabled` | `true` | Disables the `docker_context` module. |
|
| `disabled` | `false` | Disables the `docker_context` module. |
|
||||||
|
|
||||||
### Variables
|
### Variables
|
||||||
|
|
||||||
|
|
|
@ -77,3 +77,188 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::test::ModuleRenderer;
|
||||||
|
use ansi_term::Color;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn only_trigger_when_docker_config_exists() -> io::Result<()> {
|
||||||
|
let cfg_dir = tempfile::tempdir()?;
|
||||||
|
let actual = ModuleRenderer::new("docker_context")
|
||||||
|
.env("DOCKER_CONFIG", cfg_dir.path().to_string_lossy())
|
||||||
|
.collect();
|
||||||
|
let expected = None;
|
||||||
|
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
cfg_dir.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_docker_compose_yml() -> io::Result<()> {
|
||||||
|
let cfg_dir = tempfile::tempdir()?;
|
||||||
|
let cfg_file = cfg_dir.path().join("config.json");
|
||||||
|
|
||||||
|
let pwd = tempfile::tempdir()?;
|
||||||
|
File::create(pwd.path().join("docker-compose.yml"))?.sync_all()?;
|
||||||
|
|
||||||
|
let config_content = serde_json::json!({
|
||||||
|
"currentContext": "starship"
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut docker_config = File::create(&cfg_file)?;
|
||||||
|
docker_config.write_all(config_content.to_string().as_bytes())?;
|
||||||
|
docker_config.sync_all()?;
|
||||||
|
|
||||||
|
let actual = ModuleRenderer::new("docker_context")
|
||||||
|
.env("DOCKER_CONFIG", cfg_dir.path().to_string_lossy())
|
||||||
|
.path(pwd.path())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🐳 starship")));
|
||||||
|
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
|
||||||
|
cfg_dir.close()?;
|
||||||
|
pwd.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_docker_compose_yaml() -> io::Result<()> {
|
||||||
|
let cfg_dir = tempfile::tempdir()?;
|
||||||
|
let cfg_file = cfg_dir.path().join("config.json");
|
||||||
|
|
||||||
|
let pwd = tempfile::tempdir()?;
|
||||||
|
File::create(pwd.path().join("docker-compose.yaml"))?.sync_all()?;
|
||||||
|
|
||||||
|
let config_content = serde_json::json!({
|
||||||
|
"currentContext": "starship"
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut docker_config = File::create(&cfg_file)?;
|
||||||
|
docker_config.write_all(config_content.to_string().as_bytes())?;
|
||||||
|
docker_config.sync_all()?;
|
||||||
|
|
||||||
|
let actual = ModuleRenderer::new("docker_context")
|
||||||
|
.env("DOCKER_CONFIG", cfg_dir.path().to_string_lossy())
|
||||||
|
.path(pwd.path())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🐳 starship")));
|
||||||
|
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
|
||||||
|
cfg_dir.close()?;
|
||||||
|
pwd.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_dockerfile() -> io::Result<()> {
|
||||||
|
let cfg_dir = tempfile::tempdir()?;
|
||||||
|
let cfg_file = cfg_dir.path().join("config.json");
|
||||||
|
|
||||||
|
let pwd = tempfile::tempdir()?;
|
||||||
|
File::create(pwd.path().join("Dockerfile"))?.sync_all()?;
|
||||||
|
|
||||||
|
let config_content = serde_json::json!({
|
||||||
|
"currentContext": "starship"
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut docker_config = File::create(&cfg_file)?;
|
||||||
|
docker_config.write_all(config_content.to_string().as_bytes())?;
|
||||||
|
docker_config.sync_all()?;
|
||||||
|
|
||||||
|
let actual = ModuleRenderer::new("docker_context")
|
||||||
|
.env("DOCKER_CONFIG", cfg_dir.path().to_string_lossy())
|
||||||
|
.path(pwd.path())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🐳 starship")));
|
||||||
|
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
|
||||||
|
cfg_dir.close()?;
|
||||||
|
pwd.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_no_docker_files() -> io::Result<()> {
|
||||||
|
let cfg_dir = tempfile::tempdir()?;
|
||||||
|
let cfg_file = cfg_dir.path().join("config.json");
|
||||||
|
|
||||||
|
let config_content = serde_json::json!({
|
||||||
|
"currentContext": "starship"
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut docker_config = File::create(&cfg_file)?;
|
||||||
|
docker_config.write_all(config_content.to_string().as_bytes())?;
|
||||||
|
docker_config.sync_all()?;
|
||||||
|
|
||||||
|
let actual = ModuleRenderer::new("docker_context")
|
||||||
|
.env("DOCKER_CONFIG", cfg_dir.path().to_string_lossy())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let expected = None;
|
||||||
|
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
|
||||||
|
cfg_dir.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_no_scan_for_docker_files() -> io::Result<()> {
|
||||||
|
let cfg_dir = tempfile::tempdir()?;
|
||||||
|
let cfg_file = cfg_dir.path().join("config.json");
|
||||||
|
|
||||||
|
let config_content = serde_json::json!({
|
||||||
|
"currentContext": "starship"
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut docker_config = File::create(&cfg_file)?;
|
||||||
|
docker_config.write_all(config_content.to_string().as_bytes())?;
|
||||||
|
docker_config.sync_all()?;
|
||||||
|
|
||||||
|
let actual = ModuleRenderer::new("docker_context")
|
||||||
|
.env("DOCKER_CONFIG", cfg_dir.path().to_string_lossy())
|
||||||
|
.config(toml::toml! {
|
||||||
|
[docker_context]
|
||||||
|
only_with_files = false
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🐳 starship")));
|
||||||
|
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
|
||||||
|
cfg_dir.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_invalid_json() -> io::Result<()> {
|
||||||
|
let cfg_dir = tempfile::tempdir()?;
|
||||||
|
let cfg_file = cfg_dir.path().join("config.json");
|
||||||
|
|
||||||
|
let config_content = "not valid json";
|
||||||
|
|
||||||
|
let mut docker_config = File::create(&cfg_file)?;
|
||||||
|
docker_config.write_all(config_content.to_string().as_bytes())?;
|
||||||
|
docker_config.sync_all()?;
|
||||||
|
|
||||||
|
let actual = ModuleRenderer::new("docker_context")
|
||||||
|
.env("DOCKER_CONFIG", cfg_dir.path().to_string_lossy())
|
||||||
|
.config(toml::toml! {
|
||||||
|
[docker_context]
|
||||||
|
only_with_files = false
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let expected = None;
|
||||||
|
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
|
||||||
|
cfg_dir.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue