From d44c037ba565128572d881c60774ca5cd186af61 Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Fri, 21 Feb 2020 16:52:39 +0000 Subject: [PATCH] test: refactor ruby and php modules to use mocked commands (#936) --- .github/workflows/workflow.yml | 10 ------ src/modules/php.rs | 46 +++++++++++++++++++++++++ src/modules/ruby.rs | 61 ++++++++++++++++++++++++++++++++++ src/utils.rs | 10 ++++++ tests/testsuite/main.rs | 1 - tests/testsuite/php.rs | 56 ------------------------------- tests/testsuite/ruby.rs | 55 ------------------------------ 7 files changed, 117 insertions(+), 122 deletions(-) delete mode 100644 tests/testsuite/php.rs delete mode 100644 tests/testsuite/ruby.rs diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index b699be4c..8a5f2ebf 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -116,11 +116,6 @@ jobs: toolchain: stable override: true - # Install Ruby at a fixed version - - uses: eregon/use-ruby-action@v1 - with: - ruby-version: "2.6.3" - # Install Python at a fixed version - uses: actions/setup-python@v1 with: @@ -131,11 +126,6 @@ jobs: with: dotnet-version: "2.2.402" - # Install PHP at a fixed version - - uses: shivammathur/setup-php@v1 - with: - php-version: "7.3" - # Install Mercurial (pre-installed on Linux and windows) - name: Install Mercurial (macos) if: matrix.os == 'macOS-latest' diff --git a/src/modules/php.rs b/src/modules/php.rs index 4311d35f..61e464e4 100644 --- a/src/modules/php.rs +++ b/src/modules/php.rs @@ -54,10 +54,56 @@ fn format_php_version(php_version: &str) -> Option { #[cfg(test)] mod tests { use super::*; + use crate::modules::utils::test::render_module; + use ansi_term::Color; + use std::fs::File; + use std::io; + use tempfile; #[test] fn test_format_php_version() { let input = "7.3.8"; assert_eq!(format_php_version(input), Some("v7.3.8".to_string())); } + + #[test] + fn folder_without_php_files() -> io::Result<()> { + let dir = tempfile::tempdir()?; + + let actual = render_module("php", dir.path()); + + let expected = None; + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_composer_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("composer.json"))?.sync_all()?; + + let actual = render_module("php", dir.path()); + + let expected = Some(format!( + "via {} ", + Color::Fixed(147).bold().paint("🐘 v7.3.8") + )); + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_php_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("any.php"))?.sync_all()?; + + let actual = render_module("php", dir.path()); + + let expected = Some(format!( + "via {} ", + Color::Fixed(147).bold().paint("🐘 v7.3.8") + )); + assert_eq!(expected, actual); + Ok(()) + } } diff --git a/src/modules/ruby.rs b/src/modules/ruby.rs index 8556cb93..de367098 100644 --- a/src/modules/ruby.rs +++ b/src/modules/ruby.rs @@ -45,3 +45,64 @@ fn format_ruby_version(ruby_version: &str) -> Option { formatted_version.push_str(version); Some(formatted_version) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::modules::utils::test::render_module; + use ansi_term::Color; + use std::fs::File; + use std::io; + use tempfile; + + #[test] + fn folder_without_ruby_files() -> io::Result<()> { + let dir = tempfile::tempdir()?; + + let actual = render_module("ruby", dir.path()); + + let expected = None; + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_gemfile() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("Gemfile"))?.sync_all()?; + + let actual = render_module("ruby", dir.path()); + + let expected = Some(format!("via {} ", Color::Red.bold().paint("💎 v2.5.1"))); + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_rb_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("any.rb"))?.sync_all()?; + + let actual = render_module("ruby", dir.path()); + + let expected = Some(format!("via {} ", Color::Red.bold().paint("💎 v2.5.1"))); + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn test_format_ruby_version() -> io::Result<()> { + assert_eq!( + format_ruby_version("ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]"), + Some("v2.5.1".to_string()) + ); + assert_eq!( + format_ruby_version( + "ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-musl]" + ), + Some("v2.7.0".to_string()) + ); + + Ok(()) + } +} diff --git a/src/utils.rs b/src/utils.rs index c7ef6a60..5a9fbef4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -59,6 +59,16 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option { stdout: String::from("v12.0.0"), stderr: String::default(), }), + "php -r echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION;" => { + Some(CommandOutput { + stdout: String::from("7.3.8"), + stderr: String::default(), + }) + } + "ruby -v" => Some(CommandOutput { + stdout: String::from("ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]"), + stderr: String::default(), + }), "stack ghc -- --numeric-version --no-install-ghc" => Some(CommandOutput { stdout: String::from("8.6.5"), stderr: String::default(), diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index bbecc19a..572a002b 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -18,7 +18,6 @@ mod line_break; mod modules; mod nix_shell; mod python; -mod ruby; mod terraform; mod time; mod username; diff --git a/tests/testsuite/php.rs b/tests/testsuite/php.rs deleted file mode 100644 index f9836263..00000000 --- a/tests/testsuite/php.rs +++ /dev/null @@ -1,56 +0,0 @@ -use std::fs::File; -use std::io; - -use ansi_term::Color; - -use crate::common; -use crate::common::TestCommand; - -#[test] -fn folder_without_php_files() -> io::Result<()> { - let dir = common::new_tempdir()?; - - let output = common::render_module("php") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = ""; - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_composer_file() -> io::Result<()> { - let dir = common::new_tempdir()?; - File::create(dir.path().join("composer.json"))?; - - let output = common::render_module("php") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Fixed(147).bold().paint("🐘 v7.3.8")); - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_php_file() -> io::Result<()> { - let dir = common::new_tempdir()?; - File::create(dir.path().join("any.php"))?; - - let output = common::render_module("php") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Fixed(147).bold().paint("🐘 v7.3.8")); - assert_eq!(expected, actual); - Ok(()) -} diff --git a/tests/testsuite/ruby.rs b/tests/testsuite/ruby.rs deleted file mode 100644 index 7db7112c..00000000 --- a/tests/testsuite/ruby.rs +++ /dev/null @@ -1,55 +0,0 @@ -use ansi_term::Color; -use std::fs::File; -use std::io; -use tempfile; - -use crate::common; - -#[test] -fn folder_without_ruby_files() -> io::Result<()> { - let dir = tempfile::tempdir()?; - - let output = common::render_module("ruby") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = ""; - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_gemfile() -> io::Result<()> { - let dir = tempfile::tempdir()?; - File::create(dir.path().join("Gemfile"))?.sync_all()?; - - let output = common::render_module("ruby") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Red.bold().paint("💎 v2.6.3")); - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_rb_file() -> io::Result<()> { - let dir = tempfile::tempdir()?; - File::create(dir.path().join("any.rb"))?.sync_all()?; - - let output = common::render_module("ruby") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Red.bold().paint("💎 v2.6.3")); - assert_eq!(expected, actual); - Ok(()) -}