test: port Haskell module tests to integration test (#913)

This commit is contained in:
Andrew Prokhorenkov 2020-02-06 11:11:20 -06:00 committed by Matan Kushner
parent c4ab66d7fe
commit cead23edca
No known key found for this signature in database
GPG Key ID: 4B98C3A8949CA8A4
5 changed files with 31 additions and 93 deletions

View File

@ -116,23 +116,6 @@ jobs:
toolchain: stable
override: true
# Install Stack at a fixed version (Linux & macOS version)
- name: Install Stack [-nix]
if: matrix.os != 'windows-latest'
uses: mstksg/setup-stack@v1
# Install Stack at a fixed version (Windows version), use Chocolatey
- name: Install Stack [-windows]
if: matrix.os == 'windows-latest'
uses: crazy-max/ghaction-chocolatey@v1
with:
args: install haskell-stack -y
- name: Install GHC version
env:
ARGS: --resolver nightly-2019-09-21
run: stack $ARGS ghc -- --numeric-version --no-install-ghc
# Install Golang at a fixed version
- uses: actions/setup-go@v1
with:

View File

@ -22,7 +22,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
&["ghc", "--", "--numeric-version", "--no-install-ghc"],
)?
.stdout;
let formatted_version = format_haskell_version(&haskell_version)?;
let formatted_version = Some(format!("v{}", haskell_version.trim()))?;
let mut module = context.new_module("haskell");
let config: HaskellConfig = HaskellConfig::try_load(module.config);
@ -34,7 +34,30 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module)
}
fn format_haskell_version(haskell_version: &str) -> Option<String> {
let formatted_version = format!("v{}", haskell_version.trim());
Some(formatted_version)
#[cfg(test)]
mod tests {
use crate::modules::utils::test::render_module;
use ansi_term::Color;
use std::fs::File;
use std::io;
use tempfile;
#[test]
fn folder_without_stack_yaml() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let actual = render_module("haskell", dir.path());
let expected = None;
assert_eq!(expected, actual);
Ok(())
}
#[test]
fn folder_with_stack_yaml() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("stack.yaml"))?.sync_all()?;
let actual = render_module("haskell", dir.path());
let expected = Some(format!("via {} ", Color::Red.bold().paint("λ v8.6.5")));
assert_eq!(expected, actual);
Ok(())
}
}

View File

@ -43,6 +43,10 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<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(),

View File

@ -1,71 +0,0 @@
use ansi_term::Color;
use dirs::home_dir;
use std::fs::{File, OpenOptions};
use std::io::{self, Write};
use tempfile::{self, TempDir};
use crate::common;
#[test]
fn folder_without_stack_yaml() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let output = common::render_module("haskell")
.arg("--path")
.arg(dir.path())
.output()?;
let actual = String::from_utf8(output.stdout).unwrap();
let expected = "";
assert_eq!(expected, actual);
Ok(())
}
#[test]
#[ignore]
#[cfg(not(windows))]
fn folder_with_stack_yaml() -> io::Result<()> {
let dir = tempfile::tempdir()?;
create_dummy_haskell_project(&dir, Some("nightly-2019-09-21 # Last GHC 8.6.5"))?;
let output = if cfg!(windows) {
let mut app_data = home_dir().unwrap();
app_data.push("AppData");
app_data.push("Local");
eprintln!("{}", app_data.to_str().unwrap());
common::render_module("haskell")
.env("HOME", home_dir().unwrap())
.env("LOCALAPPDATA", app_data)
.env("STACK_ROOT", r"C:\sr")
.arg("--path")
.arg(dir.path())
.output()?
} else {
common::render_module("haskell")
.env("HOME", home_dir().unwrap())
.arg("--path")
.arg(dir.path())
.output()?
};
let actual = String::from_utf8(output.stdout).unwrap();
let expected = format!("via {} ", Color::Red.bold().paint("λ v8.6.5"));
assert_eq!(expected, actual);
Ok(())
}
fn create_dummy_haskell_project(folder: &TempDir, contents: Option<&str>) -> io::Result<()> {
let cabal_path = folder.path().join("test.cabal");
File::create(cabal_path)?.sync_all()?;
let stack_yaml_path = folder.path().join("stack.yaml");
let mut stack_yaml_file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&stack_yaml_path)?;
write!(stack_yaml_file, "resolver: {}", contents.unwrap_or(""))?;
stack_yaml_file.sync_data()
}

View File

@ -12,7 +12,6 @@ mod git_commit;
mod git_state;
mod git_status;
mod golang;
mod haskell;
mod hg_branch;
mod hostname;
mod jobs;