Skip to content

Commit 267b5e2

Browse files
committed
Updated tests for cfg flags. and added CI for flagging
1 parent dbc9c98 commit 267b5e2

6 files changed

Lines changed: 104 additions & 37 deletions

File tree

.github/workflows/rust.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ jobs:
4343
- name: Run tests (release)
4444
run: cargo test --release --verbose
4545

46+
no_std:
47+
name: no_std
48+
runs-on: ubuntu-latest
49+
steps:
50+
- uses: actions/checkout@v4
51+
52+
- name: Install Rust
53+
uses: dtolnay/rust-toolchain@stable
54+
55+
- name: Check no_std build
56+
run: cargo check --no-default-features
57+
58+
- name: Test no_std build
59+
run: cargo test --no-default-features
60+
4661
fmt:
4762
name: Rustfmt
4863
runs-on: ubuntu-latest

README.md

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,61 @@ CREATE TABLE users (
3333
);
3434
```
3535
A rudimentary implementation can be generated with:
36+
```rust,no_run
37+
# #[cfg(not(feature = "std"))]
38+
# fn main() {}
39+
# #[cfg(feature = "std")]
40+
fn main() -> Result<(), sql_docs::error::DocError> {
41+
use sql_docs::{GenericDialect, SqlDoc};
42+
use std::{env, fs};
43+
44+
let base = env::temp_dir().join("tmp_sql_docs_example");
45+
let _ = fs::remove_dir_all(&base);
46+
fs::create_dir_all(&base)?;
47+
let example = base.join("example.sql");
48+
49+
fs::write(
50+
&example,
51+
r#"-- Table storing user accounts
52+
-- Contains all user values
53+
/* Rows generated at registration */
54+
CREATE TABLE users (
55+
/* Primary key for each user */
56+
id INTEGER PRIMARY KEY,
57+
-- The user's login name
58+
username VARCHAR(255) NOT NULL,
59+
/* User's email address */
60+
email VARCHAR(255) UNIQUE NOT NULL
61+
);"#,
62+
)?;
63+
64+
let docs = SqlDoc::from_path(&example)
65+
.collect_all_leading()
66+
.flatten_multiline_with(". ")
67+
.build::<GenericDialect>()?;
68+
69+
let users = docs.table("users", None)?;
70+
assert_eq!(users.name(), "users");
71+
assert_eq!(
72+
users.doc(),
73+
Some("Table storing user accounts. Contains all user values. Rows generated at registration")
74+
);
75+
assert_eq!(users.path(), Some(example.as_path()));
76+
77+
let _ = fs::remove_dir_all(&base);
78+
Ok(())
79+
}
80+
```
81+
82+
Or with no `std` and from a `String`:
3683
```rust
37-
#![cfg(not(feature = "std"))]
38-
use sql_docs::{GenericDialect,SqlDoc,error::DocError};
84+
# #[cfg(feature = "std")]
85+
# fn main() {}
86+
# #[cfg(not(feature = "std"))]
87+
fn main() -> Result<(), sql_docs::error::DocError> {
88+
use sql_docs::{GenericDialect, SqlDoc};
3989

40-
fn main() -> Result<(), DocError> {
41-
// Temporary directory and file created for demonstration purposes
42-
let example =
43-
r#"-- Table storing user accounts
90+
let example = r#"-- Table storing user accounts
4491
-- Contains all user values
4592
/* Rows generated at registration */
4693
CREATE TABLE users (
@@ -52,26 +99,20 @@ CREATE TABLE users (
5299
email VARCHAR(255) UNIQUE NOT NULL
53100
);"#;
54101

55-
// Extract documentation from a single file
56-
let docs = SqlDoc::builder_from_str(&example)
57-
// Capture all valid comment lines preceding the statements directly
102+
let docs = SqlDoc::builder_from_str(example)
58103
.collect_all_leading()
59-
// Replace `\n` with a `str`
60104
.flatten_multiline_with(". ")
61-
// Finally build the `SqlDoc`
62105
.build::<GenericDialect>()?;
63-
// Or extract recursively from a directory
64-
// let docs = SqlDoc::from_dir(&base).build()?;
65106

66-
// Retrieve a specific table
67107
let users = docs.table("users", None)?;
68-
69-
// Table name
70108
assert_eq!(users.name(), "users");
71-
// Optional table-level documentation
72-
assert_eq!(users.doc(), Some("Table storing user accounts. Contains all user values. Rows generated at registration"));
109+
assert_eq!(
110+
users.doc(),
111+
Some("Table storing user accounts. Contains all user values. Rows generated at registration")
112+
);
113+
73114
Ok(())
74-
}
115+
}
75116
```
76117
## Primary Interface (Main API)
77118

src/comments.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ mod tests {
588588
let _ = std::fs::remove_dir_all(&base);
589589
Ok(())
590590
}
591-
591+
#[cfg(feature = "std")]
592592
fn assert_parsed_comments_eq(parsed: &Comments, expected: &[&str]) {
593593
let comments = parsed.comments();
594594
assert_eq!(
@@ -603,7 +603,7 @@ mod tests {
603603
assert_eq!(expected[i], comment.text(), "comment at index {i} did not match");
604604
}
605605
}
606-
606+
#[cfg(feature = "std")]
607607
fn single_line_comments_sql() -> &'static str {
608608
"-- Users table stores user account information
609609
CREATE TABLE users (
@@ -631,7 +631,7 @@ CREATE TABLE posts (
631631
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
632632
);"
633633
}
634-
634+
#[cfg(feature = "std")]
635635
fn multiline_comments_sql() -> &'static str {
636636
r"/* Users table stores user account information
637637
multiline */
@@ -670,7 +670,7 @@ CREATE TABLE posts (
670670
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
671671
);"
672672
}
673-
673+
#[cfg(feature = "std")]
674674
fn no_comments_sql() -> &'static str {
675675
"CREATE TABLE users (
676676
id INTEGER PRIMARY KEY,
@@ -687,7 +687,7 @@ CREATE TABLE posts (
687687
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
688688
);"
689689
}
690-
690+
#[cfg(feature = "std")]
691691
fn mixed_comments_sql() -> &'static str {
692692
"-- interstitial Comment above statements (should be ignored)
693693
@@ -719,7 +719,7 @@ CREATE TABLE posts (
719719
);
720720
"
721721
}
722-
722+
#[cfg(feature = "std")]
723723
fn expected_single_line_comments() -> &'static [&'static str] {
724724
&[
725725
"Users table stores user account information",
@@ -735,7 +735,7 @@ CREATE TABLE posts (
735735
"When the post was created",
736736
]
737737
}
738-
738+
#[cfg(feature = "std")]
739739
fn expected_multiline_comments() -> &'static [&'static str] {
740740
&[
741741
"Users table stores user account information\nmultiline",
@@ -751,7 +751,7 @@ CREATE TABLE posts (
751751
"When the post was created\nmultiline",
752752
]
753753
}
754-
754+
#[cfg(feature = "std")]
755755
fn expected_mixed_comments() -> &'static [&'static str] {
756756
&[
757757
"interstitial Comment above statements (should be ignored)",

src/docs.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,10 @@ mod tests {
358358
use alloc::{
359359
borrow::ToOwned,
360360
boxed::Box,
361-
string::{String, ToString},
361+
string::String,
362362
vec,
363-
vec::Vec,
364363
};
364+
#[cfg(feature = "std")]
365365
use core::fmt;
366366

367367
use sqlparser::{
@@ -391,6 +391,7 @@ mod tests {
391391
assert_eq!(sql_doc_val_column.name(), "id");
392392
}
393393

394+
#[cfg(feature = "std")]
394395
fn single_line_comments_sql() -> &'static str {
395396
"-- Users table stores user account information
396397
CREATE TABLE users (
@@ -419,6 +420,7 @@ CREATE TABLE posts (
419420
);"
420421
}
421422

423+
#[cfg(feature = "std")]
422424
fn multiline_comments_sql() -> &'static str {
423425
r"/* Users table stores user account information
424426
multiline */
@@ -458,6 +460,7 @@ CREATE TABLE posts (
458460
);"
459461
}
460462

463+
#[cfg(feature = "std")]
461464
fn no_comments_sql() -> &'static str {
462465
"CREATE TABLE users (
463466
id INTEGER PRIMARY KEY,
@@ -474,7 +477,7 @@ CREATE TABLE posts (
474477
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
475478
);"
476479
}
477-
480+
#[cfg(feature = "std")]
478481
fn mixed_comments_sql() -> &'static str {
479482
"-- interstitial Comment above statements (should be ignored)
480483
@@ -800,18 +803,20 @@ CREATE TABLE posts (
800803
Ok(())
801804
}
802805

806+
#[cfg(feature = "std")]
803807
struct FailOnNthWrite {
804808
fail_at: usize,
805809
writes: usize,
806810
sink: String,
807811
}
808-
812+
#[cfg(feature = "std")]
809813
impl FailOnNthWrite {
814+
810815
fn new(fail_at: usize) -> Self {
811816
Self { fail_at, writes: 0, sink: String::new() }
812817
}
813818
}
814-
819+
#[cfg(feature = "std")]
815820
impl fmt::Write for FailOnNthWrite {
816821
fn write_str(&mut self, s: &str) -> fmt::Result {
817822
self.writes += 1;
@@ -823,11 +828,13 @@ CREATE TABLE posts (
823828
}
824829
}
825830

831+
#[cfg(feature = "std")]
826832
fn run_fail_at<T: fmt::Display>(v: &T, fail_at: usize) -> Result<(), fmt::Error> {
827833
let mut w = FailOnNthWrite::new(fail_at);
828834
fmt::write(&mut w, format_args!("{v}"))
829835
}
830836

837+
#[cfg(feature = "std")]
831838
fn count_writes<T: fmt::Display>(v: &T) -> usize {
832839
let mut w = FailOnNthWrite { fail_at: usize::MAX, writes: 0, sink: String::new() };
833840
let _ = fmt::write(&mut w, format_args!("{v}"));

src/error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ mod tests {
136136
#[cfg(not(feature = "std"))]
137137
use alloc::{string::ToString, vec};
138138

139-
use crate::{docs::TableDoc, error::DocError};
139+
#[cfg(feature = "std")]
140+
use crate::TableDoc;
141+
use crate::error::DocError;
140142

141143
#[cfg(feature = "std")]
142144
#[test]

src/sql_doc.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,16 +521,15 @@ fn generate_docs_from_strs_with_paths<D: Dialect + Default>(
521521

522522
#[cfg(test)]
523523
mod tests {
524-
use alloc::{borrow::ToOwned, boxed::Box, vec, vec::Vec};
524+
use alloc::{boxed::Box, vec};
525525

526526
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
527527

528+
#[cfg(feature = "std")]
529+
use crate::{LeadingCommentCapture, MultiFlatten, SqlDocBuilder, docs::{ColumnDoc, TableDoc}};
528530
use crate::{
529531
SqlDoc,
530-
comments::LeadingCommentCapture,
531-
docs::{ColumnDoc, TableDoc},
532532
error::DocError,
533-
sql_doc::{MultiFlatten, SqlDocBuilder},
534533
};
535534

536535
#[cfg(feature = "std")]
@@ -642,6 +641,7 @@ mod tests {
642641
assert!(matches!(duplicate_tables_err, Err(DocError::DuplicateTablesFound { .. })));
643642
}
644643

644+
#[cfg(feature = "std")]
645645
fn sort_tables(tables: &mut [TableDoc]) {
646646
tables.sort_by(|a, b| {
647647
let a_key = (a.schema().unwrap_or(""), a.name());
@@ -759,6 +759,8 @@ mod tests {
759759
#[test]
760760
fn test_sql_builder_deny_from_path() {
761761
use std::{path::PathBuf, vec};
762+
763+
use crate::{LeadingCommentCapture, MultiFlatten, SqlDocBuilder};
762764
let actual_builder = SqlDoc::from_path("path").deny("path1").deny("path2");
763765
let expected_builder = SqlDocBuilder {
764766
source: crate::sql_doc::SqlFileDocSource::File(PathBuf::from("path")),

0 commit comments

Comments
 (0)