Skip to content

Commit c7b634c

Browse files
committed
add macros namespace test
1 parent 315882c commit c7b634c

4 files changed

Lines changed: 58 additions & 9 deletions

File tree

.github/workflows/sqlx.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ jobs:
104104
cargo test
105105
-p sqlx-macros-core
106106
--all-features
107+
108+
- name: Test sqlx-macros custom namespace
109+
run: >
110+
SQLX_NAMESPACE="external"
111+
cargo test
112+
-p sqlx-macros
113+
--all-features
114+
115+
# Should fail compilation since the env var is not set
116+
# and the testing `_macros-namespace` feature is enabled.
117+
- name: Test sqlx-macros without namespace
118+
run: >
119+
cargo test
120+
-p sqlx-macros
121+
--all-features || true
107122
108123
# Note: use `--lib` to not run integration tests that require a DB
109124
- name: Test sqlx

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqlx-macros/Cargo.toml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ _rt-tokio = ["sqlx-macros-core/_rt-tokio"]
2121
_tls-native-tls = ["sqlx-macros-core/_tls-native-tls"]
2222
_tls-rustls-aws-lc-rs = ["sqlx-macros-core/_tls-rustls-aws-lc-rs"]
2323
_tls-rustls-ring-webpki = ["sqlx-macros-core/_tls-rustls-ring-webpki"]
24-
_tls-rustls-ring-native-roots = ["sqlx-macros-core/_tls-rustls-ring-native-roots"]
24+
_tls-rustls-ring-native-roots = [
25+
"sqlx-macros-core/_tls-rustls-ring-native-roots",
26+
]
2527

2628
# SQLx features
2729
derive = ["sqlx-macros-core/derive"]
@@ -55,8 +57,14 @@ sqlx-core = { workspace = true, features = ["any"] }
5557
sqlx-macros-core = { workspace = true }
5658

5759
proc-macro2 = { version = "1.0.36", default-features = false }
58-
syn = { version = "2.0.52", default-features = false, features = ["parsing", "proc-macro"] }
60+
syn = { version = "2.0.52", default-features = false, features = [
61+
"parsing",
62+
"proc-macro",
63+
] }
5964
quote = { version = "1.0.26", default-features = false }
6065

66+
[dev-dependencies]
67+
sqlx = { workspace = true, features = ["derive", "macros", "migrate"] }
68+
6169
[lints]
62-
workspace = true
70+
workspace = true

sqlx-macros/src/lib.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ use quote::quote;
44

55
use sqlx_macros_core::*;
66

7-
/// Constant used in all macros to define the root crate.
7+
/// 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.
1010
#[cfg(not(test))]
1111
const CRATE_NAME: &str = "sqlx";
12-
// Allows for easier testing of the configurable root crate feature
12+
// Allows for easier testing of the configurable macros namespace feature
1313
// of current proc macros without duplicating them.
1414
#[cfg(test)]
15-
const CRATE_NAME: &str = match option_env!("SQLX_ROOT_CRATE") {
16-
Some(c) => c,
17-
None => "sqlx",
18-
};
15+
const CRATE_NAME: &str = env!("SQLX_NAMESPACE");
1916

2017
#[cfg(feature = "macros")]
2118
#[proc_macro]
@@ -112,3 +109,31 @@ pub fn test(args: TokenStream, input: TokenStream) -> TokenStream {
112109
}
113110
}
114111
}
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+
}

0 commit comments

Comments
 (0)