<commit_before>#![deny(warnings)]

#[macro_use]
extern crate serde_derive;
extern crate diesel;
#[macro_use]
extern crate diesel_codegen;
extern crate bufstream;
extern crate cargo_registry;
extern crate chrono;
extern crate conduit;
extern crate conduit_middleware;
extern crate conduit_test;
extern crate curl;
extern crate dotenv;
extern crate git2;
extern crate postgres;
extern crate semver;
extern crate serde;
extern crate serde_json;
extern crate time;
extern crate url;
extern crate s3;

use std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::Arc;

use cargo_registry::app::App;
use cargo_registry::category::NewCategory;
use cargo_registry::db::{self, RequestTransaction};
use cargo_registry::dependency::NewDependency;
use cargo_registry::keyword::Keyword;
use cargo_registry::krate::{NewCrate, CrateDownload};
use cargo_registry::schema::dependencies;
use cargo_registry::upload as u;
use cargo_registry::user::NewUser;
use cargo_registry::owner::{CrateOwner, NewTeam, Team};
use cargo_registry::version::NewVersion;
use cargo_registry::user::AuthenticationSource;
use cargo_registry::{User, Crate, Version, Dependency, Replica};
use conduit::{Request, Method};
use conduit_test::MockRequest;
use diesel::pg::PgConnection;
use diesel::prelude::*;
use diesel::pg::upsert::*;
use cargo_registry::schema::*;

macro_rules! t {
    ($e:expr) => (
        match $e {
            Ok(e) => e,
            Err(m) => panic!("{} failed with: {}", stringify!($e), m),
        }
    )
}

macro_rules! t_resp { ($e:expr) => (t!($e)) }

macro_rules! ok_resp {
    ($e:expr) => ({
        let resp = t_resp!($e);
        if !::ok_resp(&resp) { panic!("bad response: {:?}", resp.status); }
        resp
    })
}

macro_rules! bad_resp {
    ($e:expr) => ({
        let mut resp = t_resp!($e);
        match ::bad_resp(&mut resp) {
            None => panic!("ok response: {:?}", resp.status),
            Some(b) => b,
        }
    })
}

#[derive(Deserialize, Debug)]
struct Error {
    detail: String,
}
#[derive(Deserialize)]
struct Bad {
    errors: Vec<Error>,
}

mod badge;
mod category;
mod git;
mod keyword;
mod krate;
mod record;
mod schema_details;
mod team;
mod token;
mod user;
mod version;

fn app() -> (record::Bomb, Arc<App>, conduit_middleware::MiddlewareBuilder) {
    dotenv::dotenv().ok();
    git::init();

    let (proxy, bomb) = record::proxy();

    // When testing we route all API traffic over HTTP so we can
    // sniff/record it, but everywhere else we use https
    let api_protocol = String::from("http");

    let uploader = cargo_registry::Uploader::S3 {
        bucket: s3::Bucket::new(
            String::from("alexcrichton-test"),
            None,
            String::new(),
            String::new(),
            &api_protocol,
        ),
        proxy: Some(proxy),
    };

    let config = cargo_registry::Config {
        uploader: uploader,
        session_key: "test this has to be over 32 bytes long".to_string(),
        git_repo_checkout: git::checkout(),
        gh_client_id: env::var("GH_CLIENT_ID").unwrap_or(String::new()),
        gh_client_secret: env::var("GH_CLIENT_SECRET").unwrap_or(String::new()),
        db_url: env("TEST_DATABASE_URL"),
        env: cargo_registry::Env::Test,
        max_upload_size: 1000,
        mirror: Replica::Primary,
        api_protocol: api_protocol,
    };
    let app = App::new(&config);
    t!(t!(app.diesel_database.get()).begin_test_transaction());
    let app = Arc::new(app);
    let middleware = cargo_registry::middleware(app.clone());
    return (bomb, app, middleware);
}

fn env(s: &str) -> String {
    match env::var(s).ok() {
        Some(s) => s,
        None => panic!("must have `{}` defined", s),
    }
}

fn req(app: Arc<App>, method: conduit::Method, path: &str) -> MockRequest {
    let mut req = MockRequest::new(method, path);
    req.mut_extensions().insert(db::Transaction::new(app));
    return req;
}

fn ok_resp(r: &conduit::Response) -> bool {
    r.status.0 == 200
}

fn bad_resp(r: &mut conduit::Response) -> Option<Bad> {
    let bad = json::<Bad>(r);
    if bad.errors.len() == 0 {
        return None;
    }
    Some(bad)
}

fn json<T>(r: &mut conduit::Response) -> T
where
    for<'de> T: serde::Deserialize<'de>,
{
    let mut data = Vec::new();
    r.body.write_body(&mut data).unwrap();<commit_msg>Empty string error case for .env variables now covered<commit_after>#![deny(warnings)]

#[macro_use]
extern crate serde_derive;
extern crate diesel;
#[macro_use]
extern crate diesel_codegen;
extern crate bufstream;
extern crate cargo_registry;
extern crate chrono;
extern crate conduit;
extern crate conduit_middleware;
extern crate conduit_test;
extern crate curl;
extern crate dotenv;
extern crate git2;
extern crate postgres;
extern crate semver;
extern crate serde;
extern crate serde_json;
extern crate time;
extern crate url;
extern crate s3;

use std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::Arc;

use cargo_registry::app::App;
use cargo_registry::category::NewCategory;
use cargo_registry::db::{self, RequestTransaction};
use cargo_registry::dependency::NewDependency;
use cargo_registry::keyword::Keyword;
use cargo_registry::krate::{NewCrate, CrateDownload};
use cargo_registry::schema::dependencies;
use cargo_registry::upload as u;
use cargo_registry::user::NewUser;
use cargo_registry::owner::{CrateOwner, NewTeam, Team};
use cargo_registry::version::NewVersion;
use cargo_registry::user::AuthenticationSource;
use cargo_registry::{User, Crate, Version, Dependency, Replica};
use conduit::{Request, Method};
use conduit_test::MockRequest;
use diesel::pg::PgConnection;
use diesel::prelude::*;
use diesel::pg::upsert::*;
use cargo_registry::schema::*;

macro_rules! t {
    ($e:expr) => (
        match $e {
            Ok(e) => e,
            Err(m) => panic!("{} failed with: {}", stringify!($e), m),
        }
    )
}

macro_rules! t_resp { ($e:expr) => (t!($e)) }

macro_rules! ok_resp {
    ($e:expr) => ({
        let resp = t_resp!($e);
        if !::ok_resp(&resp) { panic!("bad response: {:?}", resp.status); }
        resp
    })
}

macro_rules! bad_resp {
    ($e:expr) => ({
        let mut resp = t_resp!($e);
        match ::bad_resp(&mut resp) {
            None => panic!("ok response: {:?}", resp.status),
            Some(b) => b,
        }
    })
}

#[derive(Deserialize, Debug)]
struct Error {
    detail: String,
}
#[derive(Deserialize)]
struct Bad {
    errors: Vec<Error>,
}

mod badge;
mod category;
mod git;
mod keyword;
mod krate;
mod record;
mod schema_details;
mod team;
mod token;
mod user;
mod version;

fn app() -> (record::Bomb, Arc<App>, conduit_middleware::MiddlewareBuilder) {
    dotenv::dotenv().ok();
    git::init();

    let (proxy, bomb) = record::proxy();

    // When testing we route all API traffic over HTTP so we can
    // sniff/record it, but everywhere else we use https
    let api_protocol = String::from("http");

    let uploader = cargo_registry::Uploader::S3 {
        bucket: s3::Bucket::new(
            String::from("alexcrichton-test"),
            None,
            String::new(),
            String::new(),
            &api_protocol,
        ),
        proxy: Some(proxy),
    };

    let config = cargo_registry::Config {
        uploader: uploader,
        session_key: "test this has to be over 32 bytes long".to_string(),
        git_repo_checkout: git::checkout(),
        gh_client_id: env::var("GH_CLIENT_ID").unwrap_or(String::new()),
        gh_client_secret: env::var("GH_CLIENT_SECRET").unwrap_or(String::new()),
        db_url: env("TEST_DATABASE_URL"),
        env: cargo_registry::Env::Test,
        max_upload_size: 1000,
        mirror: Replica::Primary,
        api_protocol: api_protocol,
    };
    let app = App::new(&config);
    t!(t!(app.diesel_database.get()).begin_test_transaction());
    let app = Arc::new(app);
    let middleware = cargo_registry::middleware(app.clone());
    return (bomb, app, middleware);
}

// Return the environment variable only if it has been defined
fn env(s: &str) -> String {
    // Handles both the `None` and empty string cases e.g. VAR=
    // by converting `None` to an empty string
    let env_result = env::var(s).ok().unwrap_or(String::new());

    if env_result == "" {
        panic!("must have `{}` defined", s);
    }

    env_result
}

fn req(app: Arc<App>, method: conduit::Method, path: &str) -> MockRequest {
    let mut req = MockRequest::new(method, path);
    req.mut_extensions().insert(db::Transaction::new(app));
    return req;
}

fn ok_resp(r: &conduit::Response) -> bool {
    r.status.0 == 200
}

fn bad_resp(r: &mut conduit::Response) -> Option<Bad> {
    let bad = json::<Bad>(r);
    if bad.errors.len() == 0 {
        return None;
    }
    Some(bad)
}

fn json<T>(r: &mut conduit::Response) -> T
where
    for<'de> T: serde::Deserialize<'de>,
{
    let mut data = Vec::new();
    r.body.write_body(&mut data).unwrap();
{'fraction_non_alphanumeric': 0.15726382931925656, 'fraction_numerical': 0.003299241174529858, 'mean_word_length': 3.7438706311945746, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2799712', 'n_tokens_mistral': 3055, 'n_tokens_neox': 2988, 'n_words': 743}