Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #131 from cloudflare/test-this-thing2
Browse files Browse the repository at this point in the history
Test this thing2
  • Loading branch information
ashleygwilliams authored May 24, 2019
2 parents e7e0a09 + a150079 commit 1f774b0
Showing 7 changed files with 152 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ which = "2.0.1"

[dev-dependencies]
assert_cmd = "0.11.1"
fs_extra = "1.1.0"

[features]
vendored-openssl = ['openssl/vendored']
69 changes: 65 additions & 4 deletions src/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
@@ -36,6 +36,10 @@ impl Bundle {
}

pub fn write(&self, wranglerjs_output: WranglerjsOutput) -> Result<(), failure::Error> {
if !Path::new(BUNDLE_OUT).exists() {
fs::create_dir(BUNDLE_OUT)?;
}

let mut metadata_file = File::create(self.metadata_path())?;
metadata_file.write_all(create_metadata(self).as_bytes())?;

@@ -114,10 +118,6 @@ pub fn run_build(
wasm_pack_path: PathBuf,
bundle: &Bundle,
) -> Result<WranglerjsOutput, failure::Error> {
if !Path::new(BUNDLE_OUT).exists() {
fs::create_dir(BUNDLE_OUT)?;
}

let mut command = Command::new(executable_path());
command.env("WASM_PACK_PATH", wasm_pack_path);

@@ -239,3 +239,64 @@ fn create_metadata(bundle: &Bundle) -> String {
.to_string()
}
}

// FIXME(sven): doesn't work because they have a race for the BUNDLE_OUT,
// make it configurable
// #[cfg(test)]
// mod tests {
// use super::*;

// #[test]
// fn it_writes_the_bundle_metadata() {
// let wranglerjs_output = WranglerjsOutput {
// script: "".to_string(),
// dist_to_clean: None,
// wasm: None,
// };
// let bundle = Bundle::new();

// bundle.write(wranglerjs_output).unwrap();
// assert!(Path::new(&bundle.metadata_path()).exists());

// cleanup(BUNDLE_OUT);
// }

// #[test]
// fn it_writes_the_bundle_script() {
// let wranglerjs_output = WranglerjsOutput {
// script: "foo".to_string(),
// dist_to_clean: None,
// wasm: None,
// };
// let bundle = Bundle::new();

// bundle.write(wranglerjs_output).unwrap();
// assert!(Path::new(&bundle.script_path()).exists());
// assert!(!Path::new(&bundle.wasm_path()).exists());

// cleanup(BUNDLE_OUT);
// }

// #[test]
// fn it_writes_the_bundle_wasm() {
// let wranglerjs_output = WranglerjsOutput {
// script: "".to_string(),
// wasm: Some("abc".to_string()),
// dist_to_clean: None,
// };
// let bundle = Bundle::new();

// bundle.write(wranglerjs_output).unwrap();
// assert!(Path::new(&bundle.wasm_path()).exists());
// assert!(bundle.has_wasm());

// cleanup(BUNDLE_OUT);
// }

// fn cleanup(name: &str) {
// let current_dir = env::current_dir().unwrap();
// let path = Path::new(&current_dir).join(name);
// println!("p: {:?}", path);
// fs::remove_dir_all(path).unwrap();
// }
// }
76 changes: 76 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use assert_cmd::prelude::*;
use fs_extra::dir::{copy, CopyOptions};
use std::env;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::process::Command;

const BUNDLE_OUT: &str = "./worker";

macro_rules! settings {
( $f:expr, $x:expr ) => {
let file_path = fixture_path($f).join("wrangler.toml");
let mut file = File::create(file_path).unwrap();
let content = format!(
r#"
name = "test"
zone_id = ""
{}
"#,
$x
);
file.write_all(content.as_bytes()).unwrap();
};
}

#[test]
fn it_builds_with_webpack_single_js() {
let fixture = "webpack_simple_js";
create_temporary_copy(fixture);

settings! {fixture, r#"
type = "Webpack"
"#};

build(fixture);
assert!(fixture_out_path(fixture).join("script.js").exists());
assert!(fixture_out_path(fixture).join("metadata.json").exists());
cleanup(fixture);
}

fn cleanup(fixture: &str) {
let path = fixture_path(fixture);
assert!(path.exists());
fs::remove_dir_all(path.clone()).unwrap();
}

fn build(fixture: &str) {
let mut build = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
println!("dir: {:?}", fixture_path(fixture));
build.current_dir(fixture_path(fixture));
build.arg("build").assert().success();
}

fn fixture_path(fixture: &str) -> PathBuf {
let mut dest = env::temp_dir();
dest.push(fixture);
dest
}

fn fixture_out_path(fixture: &str) -> PathBuf {
fixture_path(fixture).join(BUNDLE_OUT)
}

fn create_temporary_copy(fixture: &str) {
let current_dir = env::current_dir().unwrap();
let src = Path::new(&current_dir).join("tests").join(fixture);

let dest = env::temp_dir();

fs::create_dir_all(dest.clone()).unwrap();
let mut options = CopyOptions::new();
options.overwrite = true;
copy(src, dest, &options).unwrap();
}
1 change: 1 addition & 0 deletions tests/webpack_simple_js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// js
1 change: 1 addition & 0 deletions tests/webpack_simple_js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions tests/webpack_simple_js/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { entry: "./index.js" };

0 comments on commit 1f774b0

Please sign in to comment.