Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use backend specific env vars if present for tests #644

Merged
merged 1 commit into from Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/test
Expand Up @@ -2,7 +2,7 @@
set -e

if [ "$1" = "integration" ] && [ "$2" = "sqlite" ]; then
(cd diesel_tests && DATABASE_URL=/tmp/test.db cargo test --features "sqlite" --no-default-features)
(cd diesel_tests && cargo test --features "sqlite" --no-default-features)
elif [ "$1" = "integration" ]; then
(cd diesel_tests && cargo test --features "postgres" --no-default-features)
elif [ "$1" = "compile" ]; then
Expand All @@ -13,7 +13,7 @@ else
(cd diesel_infer_schema && cargo test --features "sqlite")
(cd diesel_codegen_shared && cargo test --features "sqlite")
(cd diesel_codegen && cargo test --features "sqlite")
(cd diesel_tests && DATABASE_URL=/tmp/test.db cargo test --features "sqlite" --no-default-features)
(cd diesel_tests && cargo test --features "sqlite" --no-default-features)
(cd examples && ./test_all)
(cd diesel_infer_schema && cargo test --features "postgres")
(cd diesel_codegen_shared && cargo test --features "postgres")
Expand Down
11 changes: 7 additions & 4 deletions diesel/src/doctest_setup.rs
Expand Up @@ -10,7 +10,7 @@ cfg_if! {
type DB = diesel::pg::Pg;

fn connection_no_data() -> diesel::pg::PgConnection {
let connection_url = database_url_from_env();
let connection_url = database_url_from_env("PG_DATABASE_URL");
let connection = diesel::pg::PgConnection::establish(&connection_url).unwrap();
connection.begin_test_transaction().unwrap();
connection.execute("DROP TABLE IF EXISTS users").unwrap();
Expand Down Expand Up @@ -55,7 +55,7 @@ cfg_if! {
type DB = diesel::mysql::Mysql;

fn connection_no_data() -> diesel::mysql::MysqlConnection {
let connection_url = database_url_from_env();
let connection_url = database_url_from_env("MYSQL_DATABASE_URL");
let connection = diesel::mysql::MysqlConnection::establish(&connection_url).unwrap();
connection.execute("DROP TABLE IF EXISTS users").unwrap();

Expand All @@ -81,10 +81,13 @@ cfg_if! {
}
}

fn database_url_from_env() -> String {
fn database_url_from_env(bakend_specific_env_var: &str) -> String {
use std::env;

dotenv().ok();

::std::env::var("DATABASE_URL")
env::var(bakend_specific_env_var)
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests")
}

Expand Down
4 changes: 3 additions & 1 deletion diesel/src/pg/connection/mod.rs
Expand Up @@ -207,7 +207,9 @@ mod tests {

fn connection() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL").unwrap();
let database_url = env::var("PG_DATABASE_URL")
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
PgConnection::establish(&database_url).unwrap()
}
}
3 changes: 2 additions & 1 deletion diesel/src/pg/expression/extensions/interval_dsl.rs
Expand Up @@ -259,7 +259,8 @@ mod tests {
fn $test_name(val: $tpe) -> bool {
dotenv().ok();

let connection_url = ::std::env::var("DATABASE_URL")
let connection_url = ::std::env::var("PG_DATABASE_URL")
.or_else(|_| ::std::env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
let connection = PgConnection::establish(&connection_url).unwrap();

Expand Down
3 changes: 2 additions & 1 deletion diesel/src/pg/types/date_and_time/chrono.rs
Expand Up @@ -158,7 +158,8 @@ mod tests {
fn connection() -> PgConnection {
dotenv().ok();

let connection_url = ::std::env::var("DATABASE_URL")
let connection_url = ::std::env::var("PG_DATABASE_URL")
.or_else(|_| ::std::env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
PgConnection::establish(&connection_url).unwrap()
}
Expand Down
3 changes: 2 additions & 1 deletion diesel/src/pg/types/date_and_time/std_time.rs
Expand Up @@ -80,7 +80,8 @@ mod tests {
fn connection() -> PgConnection {
dotenv().ok();

let connection_url = ::std::env::var("DATABASE_URL")
let connection_url = ::std::env::var("PG_DATABASE_URL")
.or_else(|_| ::std::env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
PgConnection::establish(&connection_url).unwrap()
}
Expand Down
10 changes: 6 additions & 4 deletions diesel/src/test_helpers.rs
Expand Up @@ -21,8 +21,9 @@ cfg_if! {

pub fn connection() -> TestConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set to run tests");
let database_url = env::var("PG_DATABASE_URL")
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
let conn = PgConnection::establish(&database_url).unwrap();
conn.begin_test_transaction().unwrap();
conn
Expand All @@ -45,8 +46,9 @@ cfg_if! {

pub fn connection_no_transaction() -> TestConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set to run tests");
let database_url = env::var("MYSQL_DATABASE_URL")
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
MysqlConnection::establish(&database_url).unwrap()
}
} else {
Expand Down
8 changes: 5 additions & 3 deletions diesel_infer_schema/src/information_schema.rs
Expand Up @@ -164,14 +164,16 @@ pub fn load_table_names<Conn>(connection: &Conn, schema_name: Option<&str>)
mod tests {
extern crate dotenv;

use super::*;
use self::dotenv::dotenv;
use diesel::pg::PgConnection;
use self::dotenv::dotenv;
use std::env;
use super::*;

fn connection() -> PgConnection {
let _ = dotenv();

let connection_url = ::std::env::var("DATABASE_URL")
let connection_url = env::var("PG_DATABASE_URL")
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
let connection = PgConnection::establish(&connection_url).unwrap();
connection.begin_test_transaction().unwrap();
Expand Down
29 changes: 18 additions & 11 deletions diesel_tests/build.rs
Expand Up @@ -2,36 +2,29 @@ extern crate diesel;
extern crate dotenv;
use self::diesel::*;
use self::dotenv::dotenv;
use std::io;
use std::{io, env};

#[cfg(feature = "postgres")]
use self::diesel::pg::PgConnection;
#[cfg(feature = "postgres")]
fn connection() -> PgConnection {
dotenv().ok();
let database_url = ::std::env::var("DATABASE_URL")
.expect("DATABASE_URL must be set to run tests");
let database_url = database_url_from_env("PG_DATABASE_URL");
PgConnection::establish(&database_url).unwrap()
}

#[cfg(feature = "sqlite")]
use self::diesel::sqlite::SqliteConnection;
#[cfg(feature = "sqlite")]
fn connection() -> SqliteConnection {
dotenv().ok();
let database_url = ::std::env::var("DATABASE_URL")
.expect("DATABASE_URL must be set to run tests");
let database_url = database_url_from_env("SQLITE_DATABASE_URL");
SqliteConnection::establish(&database_url).unwrap()
}

#[cfg(feature = "mysql")]
use self::diesel::mysql::MysqlConnection;
#[cfg(feature = "mysql")]
fn connection() -> MysqlConnection {
dotenv().ok();
let database_url = ::std::env::var("MYSQL_DATABASE_URL")
.or_else(|_| ::std::env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set to run tests");
let database_url = database_url_from_env("MYSQL_DATABASE_URL");
MysqlConnection::establish(&database_url).unwrap()
}

Expand All @@ -44,6 +37,20 @@ const MIGRATION_SUBDIR: &'static str = "sqlite";
#[cfg(feature = "mysql")]
const MIGRATION_SUBDIR: &'static str = "mysql";

fn database_url_from_env(backend_specific_env_var: &str) -> String {
dotenv().ok();
match env::var(backend_specific_env_var) {
Ok(val) => {
println!(r#"cargo:rustc-cfg=feature="backend_specific_database_url""#);
val
}
_ => {
env::var("DATABASE_URL")
.expect("DATABASE_URL must be set in order to run tests")
}
}
}

fn main() {
let migrations_dir = migrations::find_migrations_directory().unwrap().join(MIGRATION_SUBDIR);
migrations::run_pending_migrations_in_directory(&connection(), &migrations_dir, &mut io::sink()).unwrap();
Expand Down
6 changes: 6 additions & 0 deletions diesel_tests/tests/custom_schemas.rs
Expand Up @@ -3,6 +3,9 @@ use schema::connection;

mod using_infer_schema {
use super::*;
#[cfg(feature="backend_specific_database_url")]
infer_schema!("dotenv:PG_DATABASE_URL", "custom_schema");
#[cfg(not(feature="backend_specific_database_url"))]
infer_schema!("dotenv:DATABASE_URL", "custom_schema");
use self::custom_schema::users;

Expand All @@ -27,6 +30,9 @@ mod using_infer_schema {
mod using_infer_table_from_schema {
use super::*;
mod infer_users {
#[cfg(feature="backend_specific_database_url")]
infer_table_from_schema!("dotenv:PG_DATABASE_URL", "custom_schema.users");
#[cfg(not(feature="backend_specific_database_url"))]
infer_table_from_schema!("dotenv:DATABASE_URL", "custom_schema.users");
}
use self::infer_users::users;
Expand Down
10 changes: 9 additions & 1 deletion diesel_tests/tests/schema.rs
Expand Up @@ -2,6 +2,13 @@ use diesel::*;
use dotenv::dotenv;
use std::env;

#[cfg(all(feature="postgres", feature="backend_specific_database_url"))]
infer_schema!("dotenv:PG_DATABASE_URL");
#[cfg(all(feature="sqlite", feature="backend_specific_database_url"))]
infer_schema!("dotenv:SQLITE_DATABASE_URL");
#[cfg(all(feature="mysql", feature="backend_specific_database_url"))]
infer_schema!("dotenv:MYSQL_DATABASE_URL");
#[cfg(not(feature="backend_specific_database_url"))]
infer_schema!("dotenv:DATABASE_URL");

#[derive(PartialEq, Eq, Debug, Clone, Queryable, Identifiable, Insertable, AsChangeset, Associations)]
Expand Down Expand Up @@ -151,7 +158,8 @@ pub fn connection() -> TestConnection {
#[cfg(feature = "postgres")]
pub fn connection_without_transaction() -> TestConnection {
dotenv().ok();
let connection_url = env::var("DATABASE_URL")
let connection_url = env::var("PG_DATABASE_URL")
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
::diesel::pg::PgConnection::establish(&connection_url).unwrap()
}
Expand Down