test: refactor ruby and php modules to use mocked commands (#936)
This commit is contained in:
parent
c6d8031f36
commit
d44c037ba5
|
@ -116,11 +116,6 @@ jobs:
|
||||||
toolchain: stable
|
toolchain: stable
|
||||||
override: true
|
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
|
# Install Python at a fixed version
|
||||||
- uses: actions/setup-python@v1
|
- uses: actions/setup-python@v1
|
||||||
with:
|
with:
|
||||||
|
@ -131,11 +126,6 @@ jobs:
|
||||||
with:
|
with:
|
||||||
dotnet-version: "2.2.402"
|
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)
|
# Install Mercurial (pre-installed on Linux and windows)
|
||||||
- name: Install Mercurial (macos)
|
- name: Install Mercurial (macos)
|
||||||
if: matrix.os == 'macOS-latest'
|
if: matrix.os == 'macOS-latest'
|
||||||
|
|
|
@ -54,10 +54,56 @@ fn format_php_version(php_version: &str) -> Option<String> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::modules::utils::test::render_module;
|
||||||
|
use ansi_term::Color;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io;
|
||||||
|
use tempfile;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_php_version() {
|
fn test_format_php_version() {
|
||||||
let input = "7.3.8";
|
let input = "7.3.8";
|
||||||
assert_eq!(format_php_version(input), Some("v7.3.8".to_string()));
|
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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,3 +45,64 @@ fn format_ruby_version(ruby_version: &str) -> Option<String> {
|
||||||
formatted_version.push_str(version);
|
formatted_version.push_str(version);
|
||||||
Some(formatted_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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
10
src/utils.rs
10
src/utils.rs
|
@ -59,6 +59,16 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
|
||||||
stdout: String::from("v12.0.0"),
|
stdout: String::from("v12.0.0"),
|
||||||
stderr: String::default(),
|
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 {
|
"stack ghc -- --numeric-version --no-install-ghc" => Some(CommandOutput {
|
||||||
stdout: String::from("8.6.5"),
|
stdout: String::from("8.6.5"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
|
|
|
@ -18,7 +18,6 @@ mod line_break;
|
||||||
mod modules;
|
mod modules;
|
||||||
mod nix_shell;
|
mod nix_shell;
|
||||||
mod python;
|
mod python;
|
||||||
mod ruby;
|
|
||||||
mod terraform;
|
mod terraform;
|
||||||
mod time;
|
mod time;
|
||||||
mod username;
|
mod username;
|
||||||
|
|
|
@ -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(())
|
|
||||||
}
|
|
|
@ -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(())
|
|
||||||
}
|
|
Loading…
Reference in New Issue