is_dev/lib.rs
1// Copyright (c) 2018 Joseph R. Quinn <quinn.josephr@protonmail.com>
2// SPDX-License-Identifier: ISC
3
4//! [](https://travis-ci.org/quinnjr/is-dev-rs)
5//!
6//! A simple macro to determine if an environment is
7//! a "development" environment.
8//!
9//! Built because some of us (me) are lazy and don't feel like
10//! copy-pasting the same check for development
11//! environments over and over again.
12//!
13//! :heart:
14
15#![allow(unused_imports)]
16use std::env;
17use std::ffi::OsStr;
18
19#[allow(unused_macros)]
20macro_rules! is_dev {
21 ($var: expr, $m: expr) => {{
22 match env::var_os($var)
23 {
24 Some(val) => val == OsStr::new($m),
25 None => false
26 }
27 }};
28}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33
34 #[test]
35 // Only tests positive with ENV=development
36 // set on the testing machine.
37 fn test_is_dev() {
38 let e: bool = is_dev!("ENV", "development");
39 assert!(e)
40 }
41
42 #[test]
43 fn test_is_not_dev() {
44 let e: bool = is_dev!("NODE_ENV", "development");
45 assert!(!e);
46 let f: bool = is_dev!("SUPERSEKRITKEY", "development");
47 assert!(!f)
48 }
49}