From 67397d909648e7a8583cbe529e8c3ef156cf6ebd Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Wed, 12 Feb 2020 18:22:21 +0000 Subject: [PATCH] test(golang): refactor golang tests to use mocked command (#925) Refactor the golang module tests to use a mocked command and no longer depend on having a particular version of Go installed. --- .github/workflows/workflow.yml | 5 -- src/modules/golang.rs | 99 +++++++++++++++++++++++ src/utils.rs | 28 ++++--- tests/testsuite/golang.rs | 141 --------------------------------- tests/testsuite/main.rs | 1 - 5 files changed, 115 insertions(+), 159 deletions(-) delete mode 100644 tests/testsuite/golang.rs diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 124c8eea..b699be4c 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -116,11 +116,6 @@ jobs: toolchain: stable override: true - # Install Golang at a fixed version - - uses: actions/setup-go@v1 - with: - go-version: "1.12.1" - # Install Ruby at a fixed version - uses: eregon/use-ruby-action@v1 with: diff --git a/src/modules/golang.rs b/src/modules/golang.rs index e3e42b35..2f3cb27d 100644 --- a/src/modules/golang.rs +++ b/src/modules/golang.rs @@ -58,6 +58,105 @@ fn format_go_version(go_stdout: &str) -> Option { #[cfg(test)] mod tests { use super::*; + use crate::modules::utils::test::render_module; + use ansi_term::Color; + use std::fs::{self, File}; + use std::io; + use tempfile; + + #[test] + fn folder_without_go_files() -> io::Result<()> { + let dir = tempfile::tempdir()?; + + let actual = render_module("golang", dir.path()); + + let expected = None; + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_go_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("main.go"))?.sync_all()?; + + let actual = render_module("golang", dir.path()); + + let expected = Some(format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1"))); + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_go_mod() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("go.mod"))?.sync_all()?; + + let actual = render_module("golang", dir.path()); + + let expected = Some(format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1"))); + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_go_sum() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("go.sum"))?.sync_all()?; + + let actual = render_module("golang", dir.path()); + + let expected = Some(format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1"))); + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_godeps() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let godeps = dir.path().join("Godeps"); + fs::create_dir_all(&godeps)?; + + let actual = render_module("golang", dir.path()); + + let expected = Some(format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1"))); + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_glide_yaml() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("glide.yaml"))?.sync_all()?; + + let actual = render_module("golang", dir.path()); + + let expected = Some(format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1"))); + assert_eq!(expected, actual); + Ok(()) + } + + #[test] + fn folder_with_gopkg_yml() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("Gopkg.yml"))?.sync_all()?; + + let actual = render_module("golang", dir.path()); + + let expected = Some(format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1"))); + assert_eq!(expected, actual); + Ok(()) + } + #[test] + fn folder_with_gopkg_lock() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("Gopkg.lock"))?.sync_all()?; + + let actual = render_module("golang", dir.path()); + let expected = Some(format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1"))); + assert_eq!(expected, actual); + Ok(()) + } #[test] fn test_format_go_version() { diff --git a/src/utils.rs b/src/utils.rs index eba63de8..c7ef6a60 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -39,18 +39,6 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option { _ => format!("{} {}", cmd, args.join(" ")), }; match command.as_str() { - "elm --version" => Some(CommandOutput { - stdout: String::from("0.19.1"), - stderr: String::default(), - }), - "stack ghc -- --numeric-version --no-install-ghc" => Some(CommandOutput { - stdout: String::from("8.6.5"), - stderr: String::default(), - }), - "node --version" => Some(CommandOutput { - stdout: String::from("v12.0.0"), - stderr: String::default(), - }), "crystal --version" => Some(CommandOutput { stdout: String::from("Crystal 0.32.1 (2019-12-18)"), stderr: String::default(), @@ -59,6 +47,22 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option { stdout: String::from("stdout ok!"), stderr: String::from("stderr ok!"), }), + "elm --version" => Some(CommandOutput { + stdout: String::from("0.19.1"), + stderr: String::default(), + }), + "go version" => Some(CommandOutput { + stdout: String::from("go version go1.12.1 linux/amd64"), + stderr: String::default(), + }), + "node --version" => Some(CommandOutput { + stdout: String::from("v12.0.0"), + stderr: String::default(), + }), + "stack ghc -- --numeric-version --no-install-ghc" => Some(CommandOutput { + stdout: String::from("8.6.5"), + stderr: String::default(), + }), // If we don't have a mocked command fall back to executing the command _ => internal_exec_cmd(&cmd, &args), } diff --git a/tests/testsuite/golang.rs b/tests/testsuite/golang.rs deleted file mode 100644 index d415afb1..00000000 --- a/tests/testsuite/golang.rs +++ /dev/null @@ -1,141 +0,0 @@ -use ansi_term::Color; -use std::fs::{self, File}; -use std::io; -use tempfile; - -use crate::common; - -#[test] -fn folder_without_go_files() -> io::Result<()> { - let dir = tempfile::tempdir()?; - - let output = common::render_module("golang") - .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_go_file() -> io::Result<()> { - let dir = tempfile::tempdir()?; - File::create(dir.path().join("main.go"))?.sync_all()?; - - let output = common::render_module("golang") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1")); - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_go_mod() -> io::Result<()> { - let dir = tempfile::tempdir()?; - File::create(dir.path().join("go.mod"))?.sync_all()?; - - let output = common::render_module("golang") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1")); - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_go_sum() -> io::Result<()> { - let dir = tempfile::tempdir()?; - File::create(dir.path().join("go.sum"))?.sync_all()?; - - let output = common::render_module("golang") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1")); - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_godeps() -> io::Result<()> { - let dir = tempfile::tempdir()?; - let godeps = dir.path().join("Godeps"); - fs::create_dir_all(&godeps)?; - - let output = common::render_module("golang") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1")); - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_glide_yaml() -> io::Result<()> { - let dir = tempfile::tempdir()?; - File::create(dir.path().join("glide.yaml"))?.sync_all()?; - - let output = common::render_module("golang") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1")); - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_gopkg_yml() -> io::Result<()> { - let dir = tempfile::tempdir()?; - File::create(dir.path().join("Gopkg.yml"))?.sync_all()?; - - let output = common::render_module("golang") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1")); - assert_eq!(expected, actual); - Ok(()) -} - -#[test] -#[ignore] -fn folder_with_gopkg_lock() -> io::Result<()> { - let dir = tempfile::tempdir()?; - File::create(dir.path().join("Gopkg.lock"))?.sync_all()?; - - let output = common::render_module("golang") - .arg("--path") - .arg(dir.path()) - .output()?; - let actual = String::from_utf8(output.stdout).unwrap(); - - let expected = format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1")); - assert_eq!(expected, actual); - Ok(()) -} diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 8da64b56..bbecc19a 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -11,7 +11,6 @@ mod git_branch; mod git_commit; mod git_state; mod git_status; -mod golang; mod hg_branch; mod hostname; mod jobs;