2019-10-02 06:56:49 +00:00
|
|
|
use super::common;
|
2020-06-21 08:22:20 +00:00
|
|
|
use regex::Regex;
|
2019-10-02 06:56:49 +00:00
|
|
|
use std::fs::{DirBuilder, OpenOptions};
|
|
|
|
use std::io::{self, Error, ErrorKind, Write};
|
|
|
|
use std::process::{Command, Stdio};
|
2019-10-15 14:01:44 +00:00
|
|
|
use tempfile::{self, TempDir};
|
2019-10-02 06:56:49 +00:00
|
|
|
|
2020-06-21 08:22:20 +00:00
|
|
|
const DOTNET_OUTPUT_PATTERN: &str = "•NET v\\d+?\\.\\d+?\\.\\d?";
|
|
|
|
const DOTNET_PINNED_VERSION: &str = "1.2.3";
|
|
|
|
const DOTNET_PINNED_VERSION_OUTPUT_PATTERN: &str = "•NET v1\\.2\\.3";
|
2020-07-07 22:45:32 +00:00
|
|
|
const DOTNET_TFM_PATTERN: &str = r"🎯 .+";
|
|
|
|
const DOTNET_TFM_PINNED_VERSION: &str = r"netstandard2.0";
|
2020-06-21 08:22:20 +00:00
|
|
|
|
2019-10-02 06:56:49 +00:00
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_nothing_in_directory_with_zero_relevant_files() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
2020-03-15 17:12:25 +00:00
|
|
|
expect_output(&workspace, ".", None)?;
|
|
|
|
workspace.close()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 08:22:20 +00:00
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_latest_in_directory_with_directory_build_props_file() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
|
|
|
touch_path(&workspace, "Directory.Build.props", None)?;
|
|
|
|
expect_output(&workspace, ".", Some(DOTNET_OUTPUT_PATTERN))?;
|
|
|
|
workspace.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_latest_in_directory_with_directory_build_targets_file() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
|
|
|
touch_path(&workspace, "Directory.Build.targets", None)?;
|
|
|
|
expect_output(&workspace, ".", Some(DOTNET_OUTPUT_PATTERN))?;
|
|
|
|
workspace.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_latest_in_directory_with_packages_props_file() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
|
|
|
touch_path(&workspace, "Packages.props", None)?;
|
|
|
|
expect_output(&workspace, ".", Some(DOTNET_OUTPUT_PATTERN))?;
|
|
|
|
workspace.close()
|
|
|
|
}
|
|
|
|
|
2019-10-02 06:56:49 +00:00
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_latest_in_directory_with_solution() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
|
|
|
touch_path(&workspace, "solution.sln", None)?;
|
2020-06-21 08:22:20 +00:00
|
|
|
expect_output(&workspace, ".", Some(DOTNET_OUTPUT_PATTERN))?;
|
2020-03-15 17:12:25 +00:00
|
|
|
workspace.close()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_latest_in_directory_with_csproj() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
2020-07-07 22:45:32 +00:00
|
|
|
let csproj = make_csproj_with_tfm("TargetFramework", "netstandard2.0");
|
|
|
|
touch_path(&workspace, "project.csproj", Some(&csproj))?;
|
2020-06-21 08:22:20 +00:00
|
|
|
expect_output(&workspace, ".", Some(DOTNET_OUTPUT_PATTERN))?;
|
2020-07-07 22:45:32 +00:00
|
|
|
expect_output(&workspace, ".", Some(DOTNET_TFM_PATTERN))?;
|
2020-03-15 17:12:25 +00:00
|
|
|
workspace.close()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_latest_in_directory_with_fsproj() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
|
|
|
touch_path(&workspace, "project.fsproj", None)?;
|
2020-06-21 08:22:20 +00:00
|
|
|
expect_output(&workspace, ".", Some(DOTNET_OUTPUT_PATTERN))?;
|
2020-03-15 17:12:25 +00:00
|
|
|
workspace.close()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_latest_in_directory_with_xproj() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
|
|
|
touch_path(&workspace, "project.xproj", None)?;
|
2020-06-21 08:22:20 +00:00
|
|
|
expect_output(&workspace, ".", Some(DOTNET_OUTPUT_PATTERN))?;
|
2020-03-15 17:12:25 +00:00
|
|
|
workspace.close()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_latest_in_directory_with_project_json() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
|
|
|
touch_path(&workspace, "project.json", None)?;
|
2020-06-21 08:22:20 +00:00
|
|
|
expect_output(&workspace, ".", Some(DOTNET_OUTPUT_PATTERN))?;
|
2020-03-15 17:12:25 +00:00
|
|
|
workspace.close()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_pinned_in_directory_with_global_json() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
2020-06-21 08:22:20 +00:00
|
|
|
let global_json = make_pinned_sdk_json(DOTNET_PINNED_VERSION);
|
2019-10-02 06:56:49 +00:00
|
|
|
touch_path(&workspace, "global.json", Some(&global_json))?;
|
2020-06-21 08:22:20 +00:00
|
|
|
expect_output(&workspace, ".", Some(DOTNET_PINNED_VERSION_OUTPUT_PATTERN))?;
|
2020-03-15 17:12:25 +00:00
|
|
|
workspace.close()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_pinned_in_project_below_root_with_global_json() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
2020-06-21 08:22:20 +00:00
|
|
|
let global_json = make_pinned_sdk_json(DOTNET_PINNED_VERSION);
|
2020-07-07 22:45:32 +00:00
|
|
|
let csproj = make_csproj_with_tfm("TargetFramework", DOTNET_TFM_PINNED_VERSION);
|
2019-10-02 06:56:49 +00:00
|
|
|
touch_path(&workspace, "global.json", Some(&global_json))?;
|
2020-07-07 22:45:32 +00:00
|
|
|
touch_path(&workspace, "project/project.csproj", Some(&csproj))?;
|
2020-06-21 08:22:20 +00:00
|
|
|
expect_output(
|
|
|
|
&workspace,
|
|
|
|
"project",
|
|
|
|
Some(DOTNET_PINNED_VERSION_OUTPUT_PATTERN),
|
|
|
|
)?;
|
2020-03-15 17:12:25 +00:00
|
|
|
workspace.close()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_pinned_in_deeply_nested_project_within_repository() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(true)?;
|
2020-07-07 22:45:32 +00:00
|
|
|
let global_json = make_pinned_sdk_json("1.2.3");
|
|
|
|
let csproj = make_csproj_with_tfm("TargetFramework", DOTNET_TFM_PINNED_VERSION);
|
2019-10-02 06:56:49 +00:00
|
|
|
touch_path(&workspace, "global.json", Some(&global_json))?;
|
2020-07-07 22:45:32 +00:00
|
|
|
touch_path(
|
|
|
|
&workspace,
|
|
|
|
"deep/path/to/project/project.csproj",
|
|
|
|
Some(&csproj),
|
|
|
|
)?;
|
2020-06-21 08:22:20 +00:00
|
|
|
expect_output(
|
|
|
|
&workspace,
|
|
|
|
"deep/path/to/project",
|
|
|
|
Some(DOTNET_PINNED_VERSION_OUTPUT_PATTERN),
|
|
|
|
)?;
|
2020-03-15 17:12:25 +00:00
|
|
|
workspace.close()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_single_tfm() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
|
|
|
let csproj = make_csproj_with_tfm("TargetFramework", "netstandard2.0");
|
|
|
|
touch_path(&workspace, "project.csproj", Some(&csproj))?;
|
|
|
|
expect_output(&workspace, ".", Some("•NET v2.2.402"))?;
|
|
|
|
expect_output(&workspace, ".", Some("🎯 netstandard2.0"))?;
|
|
|
|
workspace.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn shows_multiple_tfms() -> io::Result<()> {
|
|
|
|
let workspace = create_workspace(false)?;
|
|
|
|
let csproj = make_csproj_with_tfm("TargetFrameworks", "netstandard2.0;net461");
|
|
|
|
touch_path(&workspace, "project.csproj", Some(&csproj))?;
|
|
|
|
expect_output(&workspace, ".", Some("•NET v2.2.402"))?;
|
|
|
|
expect_output(&workspace, ".", Some("🎯 netstandard2.0;net461"))?;
|
|
|
|
workspace.close()
|
|
|
|
}
|
|
|
|
|
2019-10-02 06:56:49 +00:00
|
|
|
fn create_workspace(is_repo: bool) -> io::Result<TempDir> {
|
2019-10-15 14:01:44 +00:00
|
|
|
let repo_dir = tempfile::tempdir()?;
|
2019-10-02 06:56:49 +00:00
|
|
|
|
|
|
|
if is_repo {
|
|
|
|
let mut command = Command::new("git");
|
|
|
|
command
|
|
|
|
.args(&["init", "--quiet"])
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.stdin(Stdio::null())
|
|
|
|
.current_dir(repo_dir.path());
|
|
|
|
|
|
|
|
if !command.status()?.success() {
|
|
|
|
return Err(Error::from(ErrorKind::Other));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(repo_dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn touch_path(workspace: &TempDir, relative_path: &str, contents: Option<&str>) -> io::Result<()> {
|
|
|
|
let path = workspace.path().join(relative_path);
|
|
|
|
|
|
|
|
DirBuilder::new().recursive(true).create(
|
|
|
|
path.parent()
|
|
|
|
.expect("Expected relative_path to be a file in a directory"),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.truncate(true)
|
|
|
|
.open(&path)?;
|
2019-10-15 15:10:16 +00:00
|
|
|
write!(file, "{}", contents.unwrap_or(""))?;
|
|
|
|
file.sync_data()
|
2019-10-02 06:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_pinned_sdk_json(version: &str) -> String {
|
|
|
|
let json_text = r#"
|
|
|
|
{
|
|
|
|
"sdk": {
|
|
|
|
"version": "INSERT_VERSION"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
json_text.replace("INSERT_VERSION", version)
|
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
fn make_csproj_with_tfm(tfm_element: &str, tfm: &str) -> String {
|
|
|
|
let json_text = r#"
|
|
|
|
<Project>
|
|
|
|
<PropertyGroup>
|
|
|
|
<TFM_ELEMENT>TFM_VALUE</TFM_ELEMENT>
|
|
|
|
</PropertyGroup>
|
|
|
|
</Project>
|
|
|
|
"#;
|
|
|
|
json_text
|
|
|
|
.replace("TFM_ELEMENT", tfm_element)
|
|
|
|
.replace("TFM_VALUE", tfm)
|
|
|
|
}
|
|
|
|
|
2020-06-21 08:22:20 +00:00
|
|
|
fn expect_output(workspace: &TempDir, run_from: &str, pattern: Option<&str>) -> io::Result<()> {
|
2019-10-02 06:56:49 +00:00
|
|
|
let run_path = workspace.path().join(run_from);
|
|
|
|
let output = common::render_module("dotnet")
|
|
|
|
.current_dir(run_path)
|
|
|
|
.output()?;
|
|
|
|
let text = String::from_utf8(output.stdout).unwrap();
|
|
|
|
|
|
|
|
// This can be helpful for debugging
|
|
|
|
eprintln!("The dotnet module showed: {}", text);
|
|
|
|
|
2020-06-21 08:22:20 +00:00
|
|
|
match pattern {
|
|
|
|
Some(pattern) => {
|
|
|
|
let re = Regex::new(pattern).unwrap();
|
|
|
|
assert!(re.is_match(&text));
|
|
|
|
}
|
2019-10-02 06:56:49 +00:00
|
|
|
None => assert!(text.is_empty()),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|