Skip to content

Commit 25b8457

Browse files
authored
Merge pull request #31 from WeareJH/no-config
fix: should support a 'default' configuration (zero config) - fixes #29
2 parents df4d8d6 + 6c7b4bb commit 25b8457

File tree

9 files changed

+131
-82
lines changed

9 files changed

+131
-82
lines changed

src/lib/config.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
extern crate serde_json;
12
extern crate serde_yaml;
23

34
use clap::Error;
45
use from_file::FromFile;
56
use from_file::FromFileError;
67
use options::ConfigError;
7-
use serde_yaml::Value;
8+
use serde_json::Value;
89
use std;
910

1011
#[derive(Deserialize, Debug, Clone)]
@@ -21,12 +22,20 @@ impl Default for ProgramConfig {
2122
impl FromFile for ProgramConfig {}
2223

2324
impl ProgramConfig {
24-
pub fn get_opts(&self, name: &str) -> Option<serde_yaml::Value> {
25+
pub fn get_opts(&self, name: &str) -> Option<serde_json::Value> {
2526
self.presets
2627
.iter()
2728
.find(|p| p.name == name)
2829
.map(|p| p.options.clone())
2930
}
31+
pub fn default_preset() -> ProgramConfig {
32+
ProgramConfig {
33+
presets: vec![PresetConfig {
34+
name: "m2".into(),
35+
options: json!({}),
36+
}],
37+
}
38+
}
3039
}
3140

3241
#[derive(Deserialize, Debug, Clone)]
@@ -39,7 +48,7 @@ pub struct PresetConfig {
3948
pub enum ProgramStartError {
4049
ConfigFileOpen,
4150
ConfigFileRead,
42-
ConfigParseError(serde_yaml::Error),
51+
ConfigParseError(serde_json::Error),
4352
ConfigCliError(ConfigError),
4453
InvalidArgs(Error),
4554
FromFile(FromFileError),

src/lib/options.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ impl ProgramOptions {
6767
Arg::with_name("config")
6868
.short("c")
6969
.long("config")
70-
.takes_value(true)
71-
.required(true),
70+
.takes_value(true),
7271
)
7372
.arg(Arg::with_name("seed").long("seed").takes_value(true))
7473
.get_matches_from_safe(args);
@@ -89,14 +88,16 @@ impl ProgramOptions {
8988
.parse()
9089
.map_err(|_e| ProgramStartError::ConfigCliError(ConfigError::UrlInvalidPort))?;
9190

92-
let config_file = matches
93-
.value_of("config")
94-
.expect("config is a required field");
95-
96-
Ok(ProgramOptions::new(host, scheme)
91+
let outgoing_opts = ProgramOptions::new(host, scheme)
9792
.with_port(port)
98-
.with_config_file(config_file)
99-
.with_seed_file(matches.value_of("seed")))
93+
.with_seed_file(matches.value_of("seed"));
94+
95+
let outgoing_opts = match matches.value_of("config") {
96+
Some(cfg_file) => outgoing_opts.with_config_file(cfg_file),
97+
None => outgoing_opts,
98+
};
99+
100+
Ok(outgoing_opts)
100101
}
101102
pub fn with_port(mut self, port: u16) -> ProgramOptions {
102103
self.port = port;

src/lib/presets/m2/bundle_config.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ extern crate serde_yaml;
22

33
use from_file::FromFile;
44

5-
#[derive(Serialize, Deserialize, Debug, Default)]
5+
#[derive(Serialize, Deserialize, Debug)]
66
pub struct BundleConfig {
77
pub bundles: Vec<ConfigItem>,
88
pub module_blacklist: Option<Vec<String>>,
99
}
1010

11+
impl Default for BundleConfig {
12+
fn default() -> BundleConfig {
13+
BundleConfig {
14+
bundles: vec![],
15+
module_blacklist: None,
16+
}
17+
}
18+
}
19+
1120
impl FromFile for BundleConfig {}
1221

1322
#[derive(Serialize, Deserialize, Debug, Default)]

src/lib/presets/m2/opts.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use config::ProgramConfig;
2-
use serde_yaml;
2+
use serde_json;
33

44
#[derive(Deserialize, Debug)]
55
pub struct M2PresetOptions {
@@ -42,28 +42,34 @@ impl Default for AuthBasic {
4242

4343
impl M2PresetOptions {
4444
pub fn get_opts(prog_config: &ProgramConfig) -> Option<M2PresetOptions> {
45-
serde_yaml::from_value(prog_config.get_opts("m2")?).ok()?
45+
serde_json::from_value(prog_config.get_opts("m2")?).ok()?
4646
}
4747
}
4848

49-
#[test]
50-
fn test_parse_preset_options_all_given() {
51-
let i = r#"
52-
require_path: /js/require.js
53-
bundle_config: file:test/fixtures/bundle-config.yaml
54-
auth_basic:
55-
username: shane
56-
password: other
57-
"#;
58-
let y: M2PresetOptions = serde_yaml::from_str(&i).unwrap();
59-
assert_eq!(y.require_path, Some("/js/require.js".to_string()));
60-
}
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
use serde_yaml;
53+
54+
#[test]
55+
fn test_parse_preset_options_all_given() {
56+
let i = r#"
57+
require_path: /js/require.js
58+
bundle_config: file:test/fixtures/bundle-config.yaml
59+
auth_basic:
60+
username: shane
61+
password: other
62+
"#;
63+
let y: M2PresetOptions = serde_yaml::from_str(&i).unwrap();
64+
assert_eq!(y.require_path, Some("/js/require.js".to_string()));
65+
}
6166

62-
#[test]
63-
fn test_defaults() {
64-
let i = r#"
65-
bundle_config: "here"
66-
"#;
67-
let y: M2PresetOptions = serde_yaml::from_str(&i).unwrap();
68-
assert_eq!(y.bundle_config, Some("here".to_string()));
67+
#[test]
68+
fn test_defaults() {
69+
let i = r#"
70+
bundle_config: "here"
71+
"#;
72+
let y: M2PresetOptions = serde_yaml::from_str(&i).unwrap();
73+
assert_eq!(y.bundle_config, Some("here".to_string()));
74+
}
6975
}

src/lib/presets/m2/state.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,41 @@ pub fn gather_state(
2525

2626
let maybe_opts = M2PresetOptions::get_opts(&req.state().program_config)
2727
.expect("should clone program config");
28-
let bundle_path = maybe_opts.bundle_config;
2928

30-
match bundle_path {
31-
Some(bun_config_path) => match BundleConfig::from_file(&bun_config_path) {
32-
Ok(bundle_config) => {
33-
let module_blacklist = bundle_config.module_blacklist.clone().unwrap_or(vec![]);
34-
let mut blacklist = vec!["js-translation".to_string()];
35-
blacklist.extend(module_blacklist);
29+
let bundle_config = match maybe_opts.bundle_config {
30+
Some(bc_path) => BundleConfig::from_file(&bc_path),
31+
None => Ok(BundleConfig::default()),
32+
};
3633

37-
let filtered =
38-
RequireJsBuildConfig::drop_blacklisted(&modules.to_vec(), &blacklist);
39-
let bundle_modules = config_gen::generate_modules(filtered, bundle_config);
40-
let mut derived_build_config = RequireJsBuildConfig::default();
34+
match bundle_config {
35+
Err(e) => Err(e.to_string()),
36+
Ok(bundle_config) => {
37+
let module_blacklist = bundle_config.module_blacklist.clone().unwrap_or(vec![]);
38+
let mut blacklist = vec!["js-translation".to_string()];
39+
blacklist.extend(module_blacklist);
4140

42-
derived_build_config.deps = client_config.deps.clone();
43-
derived_build_config.map = client_config.map.clone();
44-
derived_build_config.config = client_config.config.clone();
41+
let filtered = RequireJsBuildConfig::drop_blacklisted(&modules.to_vec(), &blacklist);
42+
let bundle_modules = config_gen::generate_modules(filtered, bundle_config);
43+
let mut derived_build_config = RequireJsBuildConfig::default();
4544

46-
let mut c = client_config.paths.clone();
47-
derived_build_config.paths = RequireJsBuildConfig::strip_paths(&c);
45+
derived_build_config.deps = client_config.deps.clone();
46+
derived_build_config.map = client_config.map.clone();
47+
derived_build_config.config = client_config.config.clone();
4848

49-
let mut shims = client_config.shim.clone();
49+
let mut c = client_config.paths.clone();
50+
derived_build_config.paths = RequireJsBuildConfig::strip_paths(&c);
5051

51-
{
52-
RequireJsBuildConfig::fix_shims(&mut shims);
53-
}
52+
let mut shims = client_config.shim.clone();
5453

55-
derived_build_config.shim = shims;
54+
{
55+
RequireJsBuildConfig::fix_shims(&mut shims);
56+
}
5657

57-
derived_build_config.modules = Some(bundle_modules.clone());
58+
derived_build_config.shim = shims;
5859

59-
Ok((derived_build_config, bundle_modules))
60-
}
61-
Err(e) => Err(e.to_string()),
62-
},
63-
_ => Err("didnt match both".to_string()),
60+
derived_build_config.modules = Some(bundle_modules.clone());
61+
62+
Ok((derived_build_config, bundle_modules))
63+
}
6464
}
6565
}

src/lib/setup.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use presets::m2::preset_m2::M2Preset;
99
use presets::m2::requirejs_config::RequireJsClientConfig;
1010
use presets::m2::seed::SeedData;
1111
use proxy_transform::proxy_transform;
12-
use serde_yaml;
12+
use serde_json;
1313
use std::collections::HashMap;
1414
use std::sync::Arc;
1515
use std::sync::Mutex;
@@ -58,7 +58,7 @@ pub fn state_and_presets(
5858
match p.name.as_str() {
5959
"m2" => {
6060
let preset_opts: M2PresetOptions =
61-
serde_yaml::from_value(p.options.clone()).unwrap();
61+
serde_json::from_value(p.options.clone()).unwrap();
6262
let preset = M2Preset::new(preset_opts);
6363
presets_map.insert(index, Box::new(preset));
6464
}

src/lib/system.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@ pub fn create(opts: ProgramOptions) -> Result<(actix::SystemRunner, String), Pro
1616
//
1717
let sys = actix::System::new("https-proxy");
1818

19-
//
20-
// Get program configuration, from the input above, and
21-
// then eventually from a file
22-
//
23-
let file_path = opts
24-
.config_file
25-
.clone()
26-
.expect("config_file cannot be missing");
27-
2819
//
2920
// Pull the ProgramConfig from a file
3021
//
31-
let program_config =
32-
ProgramConfig::from_file(&file_path).map_err(|e| ProgramStartError::FromFile(e))?;
22+
let program_config = match opts.config_file.clone() {
23+
Some(cfg_path) => {
24+
ProgramConfig::from_file(&cfg_path).map_err(|e| ProgramStartError::FromFile(e))?
25+
}
26+
None => ProgramConfig::default_preset(),
27+
};
3328

3429
//
3530
// Clone server opts to be used in multi threads

tests/api.rs

+28
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,34 @@ fn test_build_json_from_json_config() {
9797
});
9898
}
9999

100+
#[test]
101+
fn test_build_json_without_config() {
102+
let args = vec!["config-gen", "http://example.com"];
103+
api(args, "/__bs/build.json", |result| {
104+
let (_sys, _url, mut res) = result.expect("api returned");
105+
let _c: RequireJsBuildConfig = serde_json::from_str(
106+
&res.text().expect("unwrap text response"),
107+
).expect("serde deserialize");
108+
});
109+
}
110+
111+
#[test]
112+
fn test_build_json_with_seed_without_config() {
113+
let args = vec![
114+
"config-gen",
115+
"http://example.com",
116+
"--seed",
117+
"test/fixtures/seed.json",
118+
];
119+
api(args, "/__bs/build.json", |result| {
120+
let (_sys, _url, mut res) = result.expect("api returned");
121+
let _c: RequireJsBuildConfig = serde_json::from_str(
122+
&res.text().expect("unwrap text response"),
123+
).expect("serde deserialize");
124+
println!("_c={:?}", _c);
125+
});
126+
}
127+
100128
///
101129
/// Test helper to run the server from a Vec of args
102130
/// just like you would in the the CLI

tests/proxy_transform_test.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn test_redirect() {
8787
});
8888
});
8989

90-
let (mut proxy, proxy_address) = get_test_proxy(&target, |app| {
90+
let (mut proxy, _proxy_address) = get_test_proxy(&target, |app| {
9191
app.handler(proxy_transform);
9292
});
9393

@@ -100,13 +100,14 @@ fn test_redirect() {
100100
.expect("finish request");
101101

102102
let (resp, _resp_body) = get_resp(&mut proxy, request);
103-
let expected_redirect = format!("http://{}/login", proxy_address);
104-
let _actual_redirect = resp
105-
.headers()
106-
.get(header::LOCATION)
107-
.expect("has location header")
108-
.to_str()
109-
.expect("header->str");
110103
assert_eq!(resp.status(), 302);
104+
105+
// let _actual_redirect = resp
106+
// .headers()
107+
// .get(header::LOCATION)
108+
// .expect("has location header")
109+
// .to_str()
110+
// .expect("header->str");
111+
// let expected_redirect = format!("http://{}/login", proxy_address);
111112
// assert_eq!(actual_redirect, expected_redirect);
112113
}

0 commit comments

Comments
 (0)