Skip to content

Commit

Permalink
tests for notion current
Browse files Browse the repository at this point in the history
  • Loading branch information
mikrostew committed Sep 7, 2018
1 parent 48d6edf commit 39c1215
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/testsuite/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ mod support;

// test files

mod notion_current;
mod notion_deactivate;
mod notion_use;
164 changes: 164 additions & 0 deletions tests/testsuite/notion_current.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
use support::hamcrest::assert_that;
use support::matchers::execs;
use support::sandbox::sandbox;

use notion_fail::ExitCode;

const BASIC_PACKAGE_JSON: &'static str = r#"{
"name": "test-package"
}"#;

fn package_json_with_pinned_node(version: &str) -> String {
format!(
r#"{{
"name": "test-package",
"toolchain": {{
"node": "{}"
}}
}}"#,
version
)
}

#[test]
fn pinned_project() {
let s = sandbox()
.package_json(&package_json_with_pinned_node("1.7.19"))
.build();

assert_that(
s.notion("current"),
execs()
.with_status(0)
.with_stdout_contains("project: v1.7.19 (active)"),
);
}

#[test]
fn pinned_project_with_user_node_env() {
let s = sandbox()
.package_json(&package_json_with_pinned_node("1.7.19"))
.env("NOTION_NODE_VERSION", "2.18.5")
.build();

assert_that(
s.notion("current"),
execs()
.with_status(0)
.with_stdout_contains("project: v1.7.19 (active)")
.with_stdout_contains("user: v2.18.5"),
);
}

#[test]
fn pinned_project_with_user_node_default() {
let s = sandbox()
.package_json(&package_json_with_pinned_node("1.7.19"))
.catalog(r#"[node]
default = '9.12.11'
versions = [ '9.12.11' ]
"#
)
.build();

assert_that(
s.notion("current"),
execs()
.with_status(0)
.with_stdout_contains("project: v1.7.19 (active)")
.with_stdout_contains("user: v9.12.11"),
);
}

#[test]
fn unpinned_project() {
let s = sandbox()
.package_json(BASIC_PACKAGE_JSON)
.build();

assert_that(
s.notion("current"),
execs()
.with_status(ExitCode::NoVersionMatch as i32)
.with_stderr("error: no versions found"),
);
}

#[test]
fn unpinned_project_with_user_node_env() {
let s = sandbox()
.package_json(BASIC_PACKAGE_JSON)
.env("NOTION_NODE_VERSION", "2.18.5")
.build();

assert_that(
s.notion("current"),
execs()
.with_status(0)
.with_stdout_contains("user: v2.18.5 (active)"),
);
}

#[test]
fn unpinned_project_with_user_node_default() {
let s = sandbox()
.package_json(BASIC_PACKAGE_JSON)
.catalog(r#"[node]
default = '9.12.11'
versions = [ '9.12.11' ]
"#
)
.build();

assert_that(
s.notion("current"),
execs()
.with_status(0)
.with_stdout_contains("user: v9.12.11 (active)"),
);
}

#[test]
fn no_project() {
let s = sandbox()
.build();

assert_that(
s.notion("current"),
execs()
.with_status(ExitCode::NoVersionMatch as i32)
.with_stderr("error: no versions found"),
);
}

#[test]
fn no_project_with_user_node_env() {
let s = sandbox()
.env("NOTION_NODE_VERSION", "2.18.5")
.build();

assert_that(
s.notion("current"),
execs()
.with_status(0)
.with_stdout_contains("user: v2.18.5 (active)"),
);
}

#[test]
fn no_project_with_user_node_default() {
let s = sandbox()
.catalog(r#"[node]
default = '9.12.11'
versions = [ '9.12.11' ]
"#
)
.build();

assert_that(
s.notion("current"),
execs()
.with_status(0)
.with_stdout_contains("user: v9.12.11 (active)"),
);
}
57 changes: 57 additions & 0 deletions tests/testsuite/support/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,40 @@ impl EnvVar {
}
}

// catalog.toml
#[derive(PartialEq, Clone)]
pub struct CatalogBuilder {
path: PathBuf,
contents: String,
}

impl CatalogBuilder {
pub fn new(path: PathBuf, contents: &str) -> CatalogBuilder {
CatalogBuilder {
path,
contents: contents.to_string(),
}
}

pub fn build(&self) {
self.dirname().mkdir_p();

let mut file = File::create(&self.path).unwrap_or_else(|e| {
panic!(
"could not create catalog.toml file {}: {}",
self.path.display(),
e
)
});

t!(file.write_all(self.contents.as_bytes()));
}

fn dirname(&self) -> &Path {
self.path.parent().unwrap()
}
}

#[must_use]
pub struct SandboxBuilder {
root: Sandbox,
Expand All @@ -128,6 +162,7 @@ pub struct SandboxBuilder {
node_archive_mock: Option<String>,
yarn_archive_mock: Option<String>,
path_dirs: Vec<PathBuf>,
catalog: Option<CatalogBuilder>,
}

impl SandboxBuilder {
Expand All @@ -152,6 +187,7 @@ impl SandboxBuilder {
node_archive_mock: None,
yarn_archive_mock: None,
path_dirs: vec![notion_bin_dir()],
catalog: None,
}
}

Expand Down Expand Up @@ -180,12 +216,25 @@ impl SandboxBuilder {
self
}

/// Set an environment variable for the sandbox (chainable)
pub fn env(mut self, name: &str, value: &str) -> Self {
self.root.env_vars.push(EnvVar::new(name, value));
self
}

/// Add a directory to the PATH (chainable)
pub fn path_dir(mut self, dir: &str) -> Self {
self.path_dirs.push(PathBuf::from(dir));
self
}

/// Set the catalog.toml for the sandbox (chainable)
pub fn catalog(mut self, contents: &str) -> Self {
let catalog_file = user_catalog_file();
self.catalog = Some(CatalogBuilder::new(catalog_file, contents));
self
}

/// Create the project
pub fn build(mut self) -> Sandbox {
// First, clean the directory if it already exists
Expand All @@ -212,6 +261,11 @@ impl SandboxBuilder {
cache.build();
}

// write catalog
if let Some(catalog_builder) = self.catalog {
catalog_builder.build();
}

// setup network mocks
if let Some(_mock) = self.node_index_mock {
panic!("unimplemented!!"); // TODO
Expand Down Expand Up @@ -294,6 +348,9 @@ fn package_json_file(mut root: PathBuf) -> PathBuf {
root.push("package.json");
root
}
fn user_catalog_file() -> PathBuf {
notion_home().join("catalog.toml")
}

fn fixture_dir() -> PathBuf {
let mut cargo_manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down

0 comments on commit 39c1215

Please sign in to comment.