Skip to content

Commit 5a24918

Browse files
committed
add build.rs watching; use internal cfg for testing
1 parent 4472e9d commit 5a24918

5 files changed

Lines changed: 37 additions & 33 deletions

File tree

.github/workflows/sqlx.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,22 @@ jobs:
107107
108108
- name: Test sqlx-macros custom namespace
109109
run: >
110-
SQLX_NAMESPACE="external"
111110
cargo test
112111
-p sqlx-macros
113112
--all-features
113+
env:
114+
SQLX_NAMESPACE: "external"
115+
RUSTFLAGS: "--cfg sqlx_macros_namespace"
114116

115117
# Should fail compilation since the env var is not set
116-
# and the testing `_macros-namespace` feature is enabled.
118+
# and the testing `sqlx_macros_namespace` cfg is enabled.
117119
- name: Test sqlx-macros without namespace
118120
run: >
119121
cargo test
120122
-p sqlx-macros
121123
--all-features || true
124+
env:
125+
RUSTFLAGS: "--cfg sqlx_macros_namespace"
122126

123127
# Note: use `--lib` to not run integration tests that require a DB
124128
- name: Test sqlx

sqlx-macros/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ syn = { version = "2.0.52", default-features = false, features = [
6464
quote = { version = "1.0.26", default-features = false }
6565

6666
[dev-dependencies]
67-
sqlx = { workspace = true, features = ["derive", "macros", "migrate"] }
67+
sqlx = { workspace = true, features = ["derive", "macros", "migrate", "postgres"] }
68+
69+
[[test]]
70+
name = "macros_namespace"
71+
path = "tests/macros_namespace.rs"
6872

6973
[lints]
7074
workspace = true

sqlx-macros/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
println!("cargo::rustc-check-cfg=cfg(sqlx_macros_namespace)");
3+
println!("cargo:rerun-if-env-changed=SQLX_NAMESPACE");
4+
}

sqlx-macros/src/lib.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use sqlx_macros_core::*;
77
/// Constant used in all macros to define the macros namespace.
88
/// This accommodates 3rd party drivers by allowing them to specify a different
99
/// root crate that paths used with proc macros resolve to.
10-
#[cfg(not(test))]
10+
#[cfg(not(sqlx_macros_namespace))]
1111
const CRATE_NAME: &str = "sqlx";
1212
// Allows for easier testing of the configurable macros namespace feature
1313
// of current proc macros without duplicating them.
14-
#[cfg(test)]
14+
#[cfg(sqlx_macros_namespace)]
1515
const CRATE_NAME: &str = env!("SQLX_NAMESPACE");
1616

1717
#[cfg(feature = "macros")]
@@ -109,31 +109,3 @@ pub fn test(args: TokenStream, input: TokenStream) -> TokenStream {
109109
}
110110
}
111111
}
112-
113-
#[cfg(test)]
114-
mod tests {
115-
#[test]
116-
#[cfg(feature = "migrate")]
117-
fn test_macros_namespace_migrate() {
118-
/// Import as different namespace.
119-
///
120-
/// This must be set as `SQLX_NAMESPACE` environment variable to test that
121-
/// changing the namespace still results in the proc macros behaving well.
122-
extern crate sqlx as external;
123-
124-
let _ = external::migrate!("../tests/migrate/migrations_simple");
125-
}
126-
127-
#[test]
128-
#[cfg(feature = "derive")]
129-
fn test_macros_namespace_derive() {
130-
/// Import as different namespace.
131-
///
132-
/// This must be set as `SQLX_NAMESPACE` environment variable to test that
133-
/// changing the namespace still results in the proc macros behaving well.
134-
extern crate sqlx as external;
135-
136-
#[derive(Debug, external::Type, external::Encode, external::Decode, external::FromRow)]
137-
struct Test {}
138-
}
139-
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// Import as different namespace.
2+
///
3+
/// This must be set as `SQLX_NAMESPACE` environment variable to test that
4+
/// changing the namespace still results in the proc macros behaving well.
5+
extern crate sqlx as external;
6+
7+
#[test]
8+
#[cfg(feature = "migrate")]
9+
fn test_macros_namespace_migrate() {
10+
let _ = external::migrate!("../tests/migrate/migrations_simple");
11+
}
12+
13+
#[test]
14+
#[cfg(feature = "derive")]
15+
fn test_macros_namespace_derive() {
16+
#[derive(Debug, external::Type, external::FromRow)]
17+
struct Test {
18+
value: i32,
19+
}
20+
}

0 commit comments

Comments
 (0)