Skip to content

Commit

Permalink
chore: make wasm build again (#141)
Browse files Browse the repository at this point in the history
* chore: make wasm build again

* chore: make hostname a feature instead

* fix: put tests behind feature, include env when needed

* refactor: organize imports slightly differently

* chore: test all features
  • Loading branch information
nunogois authored Sep 6, 2024
1 parent f8de91a commit 33cd400
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build-wasm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ jobs:
uses: jetli/wasm-pack-action@v0.3.0

- name: Build WASM
run: wasm-pack build --target nodejs
run: |
cd wasm-engine
wasm-pack build --target nodejs
2 changes: 1 addition & 1 deletion .github/workflows/sarif-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ jobs:
path: client-specification
- name: Run tests
run: |
cargo test
cargo test --all-features
2 changes: 1 addition & 1 deletion unleash-yggdrasil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ convert_case = "0.6.0"
unleash-types = "0.13.0"
chrono = "0.4.38"
dashmap = "5.5.0"
hostname = "0.3.1"
hostname = { version = "0.3.1", optional = true }
ipnetwork = "0.20.0"

[dependencies.serde]
Expand Down
64 changes: 39 additions & 25 deletions unleash-yggdrasil/src/strategy_parsing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate pest;

use std::collections::HashSet;
use std::env;
use std::io::Cursor;
use std::net::IpAddr;
use std::num::ParseFloatError;
Expand All @@ -11,7 +10,6 @@ use crate::sendable_closures::{SendableContextResolver, SendableFragment};
use crate::state::SdkError;
use crate::EnrichedContext as Context;
use chrono::{DateTime, Utc};
use hostname;
use ipnetwork::{IpNetwork, IpNetworkError};
use murmur3::murmur3_32;
use pest::iterators::{Pair, Pairs};
Expand All @@ -20,6 +18,11 @@ use pest::Parser;
use rand::Rng;
use semver::Version;

#[cfg(feature = "hostname")]
use std::env;
#[cfg(feature = "hostname")]
use hostname;

#[derive(Parser)]
#[grammar = "strategy_grammar.pest"]
struct Strategy;
Expand Down Expand Up @@ -399,6 +402,7 @@ fn rollout_constraint(node: Pairs<Rule>) -> CompileResult<RuleFragment> {
}))
}

#[cfg(feature = "hostname")]
fn get_hostname() -> CompileResult<String> {
//This is primarily for testing purposes
if let Ok(hostname_env) = env::var("hostname") {
Expand All @@ -414,6 +418,11 @@ fn get_hostname() -> CompileResult<String> {
})
}

#[cfg(not(feature = "hostname"))]
fn get_hostname() -> CompileResult<String> {
Err(SdkError::StrategyParseError("Hostname is not supported on this platform".into()))
}

fn hostname_constraint(node: Pairs<Rule>) -> CompileResult<RuleFragment> {
let [hostname_node] = drain(node)?;

Expand Down Expand Up @@ -1066,37 +1075,42 @@ mod tests {
assert!(rule(&context));
}

#[test]
fn evaluates_host_name_constraint_correctly() {
std::env::set_var("hostname", "DOS");
#[cfg(feature = "hostname")]
mod hostname_tests {
use super::*;

#[test]
fn evaluates_host_name_constraint_correctly() {
std::env::set_var("hostname", "DOS");

let rule = compile_rule("hostname in [\"DOS\"]").unwrap();
let context = Context::default();
assert!(rule(&context));
let rule = compile_rule("hostname in [\"DOS\"]").unwrap();
let context = Context::default();
assert!(rule(&context));

std::env::remove_var("hostname");
}
std::env::remove_var("hostname");
}

#[test]
fn evaluates_host_name_to_false_when_missing_hostname_values() {
std::env::set_var("hostname", "DOS");
#[test]
fn evaluates_host_name_to_false_when_missing_hostname_values() {
std::env::set_var("hostname", "DOS");

let rule = compile_rule("hostname in [\"\"]").unwrap();
let context = Context::default();
assert!(!rule(&context));
let rule = compile_rule("hostname in [\"\"]").unwrap();
let context = Context::default();
assert!(!rule(&context));

std::env::remove_var("hostname");
}
std::env::remove_var("hostname");
}

#[test]
fn hostname_constraint_ignores_casing() {
std::env::set_var("hostname", "DaRWin");
#[test]
fn hostname_constraint_ignores_casing() {
std::env::set_var("hostname", "DaRWin");

let rule = compile_rule("hostname in [\"dArWin\", \"pop-os\"]").unwrap();
let context = Context::default();
assert!(rule(&context));
let rule = compile_rule("hostname in [\"dArWin\", \"pop-os\"]").unwrap();
let context = Context::default();
assert!(rule(&context));

std::env::remove_var("hostname");
std::env::remove_var("hostname");
}
}

#[test_case("127.0.0.1", "127.0.0.1", true; "Exact match")]
Expand Down
2 changes: 1 addition & 1 deletion wasm-engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ yarn add @unleash/yggdrasil-engine
Then, you can use it in your code:

```js
const yggdrasil = require("../pkg/wasm_engine.js");
const yggdrasil = require("../pkg/yggdrasil_engine.js");

const context = {
userId: "7",
Expand Down
2 changes: 1 addition & 1 deletion wasm-engine/e2e-tests/wasm-engine.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const yggdrasil = require("../pkg/wasm_engine.js");
const yggdrasil = require("../pkg/yggdrasil_engine.js");

test("Rule evaluates correctly", () => {
const context = {
Expand Down
4 changes: 2 additions & 2 deletions wasm-engine/tests/web.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Test suite for the Web and headless browsers.

#![cfg(target_arch = "wasm32")]
#![cfg(target_family = "wasm")]

extern crate wasm_bindgen_test;
use serde_wasm_bindgen::to_value;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use wasm_engine::{evaluate, Context};
use yggdrasil_engine::{evaluate, Context};

#[wasm_bindgen_test]
fn dsl_evaluates_correct() {
Expand Down

0 comments on commit 33cd400

Please sign in to comment.