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.
This commit is contained in:
Thomas O'Donnell 2020-02-12 18:22:21 +00:00 committed by GitHub
parent f8615f9a36
commit 67397d9096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 115 additions and 159 deletions

View File

@ -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:

View File

@ -58,6 +58,105 @@ fn format_go_version(go_stdout: &str) -> Option<String> {
#[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() {

View File

@ -39,18 +39,6 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
_ => 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<CommandOutput> {
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),
}

View File

@ -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(())
}

View File

@ -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;