1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
mod wrangler_toml;
pub use wrangler_toml::{EnvConfig, KvConfig, SiteConfig, Triggers, WranglerToml, TEST_ENV_NAME};

use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::mem::ManuallyDrop;
use std::path::PathBuf;
use std::thread;

use tempfile::TempDir;

const BUNDLE_OUT: &str = "worker";

pub struct Fixture {
    // we wrap the fixture's tempdir in a `ManuallyDrop` so that if a test
    // fails, its directory isn't deleted, and we have a chance to manually
    // inspect its state and figure out what is going on.
    dir: ManuallyDrop<TempDir>,
    output_path: &'static str,
}

impl Default for Fixture {
    fn default() -> Self {
        Self::new()
    }
}

impl Fixture {
    pub fn new() -> Fixture {
        let dir = TempDir::new().unwrap();
        eprintln!("Created fixture at {}", dir.path().display());
        Fixture {
            dir: ManuallyDrop::new(dir),
            output_path: BUNDLE_OUT,
        }
    }

    pub fn new_site() -> Fixture {
        let mut fixture = Fixture::new();
        fixture.output_path = "workers-site/worker";

        fixture.scaffold_site();

        fixture
    }

    pub fn get_path(&self) -> PathBuf {
        self.dir.path().to_path_buf()
    }

    pub fn scaffold_webpack(&self) {
        self.create_default_package_json();
        self.create_empty_js();
    }

    pub fn get_output_path(&self) -> PathBuf {
        self.get_path().join(self.output_path)
    }

    pub fn create_file(&self, name: &str, content: &str) {
        let file_path = self.get_path().join(name);
        let mut file = File::create(file_path).unwrap();
        let content = String::from(content);
        file.write_all(content.as_bytes()).unwrap();
    }

    pub fn create_dir(&self, name: &str) {
        let dir_path = self.get_path().join(name);
        fs::create_dir(dir_path).unwrap();
    }

    pub fn create_empty_js(&self) {
        self.create_file("index.js", "");
    }

    pub fn create_default_package_json(&self) {
        self.create_file(
            "package.json",
            r#"
            {
                "main": "index.js"
            }
        "#,
        );
    }

    pub fn create_wrangler_toml(&self, wrangler_toml: WranglerToml) {
        self.create_file("wrangler.toml", &toml::to_string(&wrangler_toml).unwrap());
    }

    pub fn scaffold_site(&self) {
        self.create_dir("workers-site");
        self.create_file(
            "workers-site/package.json",
            r#"
            {
              "private": true,
              "main": "index.js",
              "dependencies": {
                "@cloudflare/kv-asset-handler": "^0.0.5"
              }
            }
        "#,
        );
        self.create_file("workers-site/index.js", "");
    }
}

impl Drop for Fixture {
    fn drop(&mut self) {
        if !thread::panicking() {
            unsafe { ManuallyDrop::drop(&mut self.dir) }
        }
    }
}