Skip to content

Commit 683f078

Browse files
committed
updated tests. No longer reliant on sql files in dir
1 parent 4f7f4ef commit 683f078

File tree

7 files changed

+298
-129
lines changed

7 files changed

+298
-129
lines changed

comments.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
4+
5+
6+
7+
8+

sql_files/with_mixed_comments.sql

Lines changed: 0 additions & 28 deletions
This file was deleted.

sql_files/with_multiline_comments.sql

Lines changed: 0 additions & 36 deletions
This file was deleted.

sql_files/with_single_line_comments.sql

Lines changed: 0 additions & 25 deletions
This file was deleted.

sql_files/without_comments.sql

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/comments.rs

Lines changed: 155 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ impl Comments {
364364

365365
#[cfg(test)]
366366
mod tests {
367+
use std::{env, fs};
368+
367369
use crate::comments::{Comment, CommentError, CommentKind, Comments, Location, Span};
368370

369371
#[test]
@@ -424,10 +426,22 @@ mod tests {
424426
#[test]
425427
fn parse_comments() -> Result<(), Box<dyn std::error::Error>> {
426428
use crate::{ast::ParsedSqlFileSet, comments::Comments, files::SqlFileSet};
427-
use std::path::Path;
428-
429-
let path = Path::new("sql_files");
430-
let set = SqlFileSet::new(path, &[])?;
429+
let base = env::temp_dir().join("all_sql_files");
430+
let _ = fs::remove_dir_all(&base);
431+
fs::create_dir_all(&base)?;
432+
let file1 = base.join("with_single_line_comments.sql");
433+
fs::File::create(&file1)?;
434+
fs::write(&file1, single_line_comments_sql())?;
435+
let file2 = base.join("with_multiline_comments.sql");
436+
fs::File::create(&file2)?;
437+
fs::write(&file2, multiline_comments_sql())?;
438+
let file3 = base.join("with_mixed_comments.sql");
439+
fs::File::create(&file3)?;
440+
fs::write(&file3, mixed_comments_sql())?;
441+
let file4 = base.join("without_comments.sql");
442+
fs::File::create(&file4)?;
443+
fs::write(&file4, no_comments_sql())?;
444+
let set = SqlFileSet::new(&base, &[])?;
431445
let parsed_set = ParsedSqlFileSet::parse_all(set)?;
432446

433447
for file in parsed_set.files() {
@@ -459,7 +473,7 @@ mod tests {
459473
}
460474
}
461475
}
462-
476+
let _ = fs::remove_dir_all(&base);
463477
Ok(())
464478
}
465479

@@ -478,6 +492,122 @@ mod tests {
478492
}
479493
}
480494

495+
fn single_line_comments_sql() -> &'static str {
496+
"-- Users table stores user account information
497+
CREATE TABLE users (
498+
-- Primary key
499+
id INTEGER PRIMARY KEY,
500+
-- Username for login
501+
username VARCHAR(255) NOT NULL,
502+
-- Email address
503+
email VARCHAR(255) UNIQUE NOT NULL,
504+
-- When the user registered
505+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
506+
);
507+
508+
-- Posts table stores blog posts
509+
CREATE TABLE posts (
510+
-- Primary key
511+
id INTEGER PRIMARY KEY,
512+
-- Post title
513+
title VARCHAR(255) NOT NULL,
514+
-- Foreign key linking to users
515+
user_id INTEGER NOT NULL,
516+
-- Main body text
517+
body TEXT NOT NULL,
518+
-- When the post was created
519+
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
520+
);"
521+
}
522+
523+
fn multiline_comments_sql() -> &'static str {
524+
r#"/* Users table stores user account information
525+
multiline */
526+
CREATE TABLE users (
527+
/* Primary key
528+
multiline */
529+
id INTEGER PRIMARY KEY,
530+
/* Username for login
531+
multiline */
532+
username VARCHAR(255) NOT NULL,
533+
/* Email address
534+
multiline */
535+
email VARCHAR(255) UNIQUE NOT NULL,
536+
/* When the user registered
537+
multiline */
538+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
539+
);
540+
541+
/* Posts table stores blog posts
542+
multiline */
543+
CREATE TABLE posts (
544+
/* Primary key
545+
multiline */
546+
id INTEGER PRIMARY KEY,
547+
/* Post title
548+
multiline */
549+
title VARCHAR(255) NOT NULL,
550+
/* Foreign key linking to users
551+
multiline */
552+
user_id INTEGER NOT NULL,
553+
/* Main body text
554+
multiline */
555+
body TEXT NOT NULL,
556+
/* When the post was created
557+
multiline */
558+
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
559+
);"#
560+
}
561+
562+
fn no_comments_sql() -> &'static str {
563+
"CREATE TABLE users (
564+
id INTEGER PRIMARY KEY,
565+
username VARCHAR(255) NOT NULL,
566+
email VARCHAR(255) UNIQUE NOT NULL,
567+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
568+
);
569+
570+
CREATE TABLE posts (
571+
id INTEGER PRIMARY KEY,
572+
title VARCHAR(255) NOT NULL,
573+
user_id INTEGER NOT NULL,
574+
body TEXT NOT NULL,
575+
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
576+
);"
577+
}
578+
579+
fn mixed_comments_sql() -> &'static str {
580+
"-- interstitial Comment above statements (should be ignored)
581+
582+
/* Users table stores user account information */
583+
CREATE TABLE users ( /* users interstitial comment
584+
(should be ignored) */
585+
-- Primary key
586+
id INTEGER PRIMARY KEY, -- Id comment that is interstitial (should be ignored)
587+
/* Username for login */
588+
username VARCHAR(255) NOT NULL,
589+
-- Email address
590+
email VARCHAR(255) UNIQUE NOT NULL,
591+
/* When the user registered */
592+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
593+
);
594+
595+
/* Posts table stores blog posts */
596+
CREATE TABLE posts (
597+
-- Primary key
598+
id INTEGER PRIMARY KEY,
599+
/* Post title */
600+
title VARCHAR(255) NOT NULL,
601+
-- Foreign key linking to users
602+
user_id INTEGER NOT NULL,
603+
/* Main body text */
604+
body TEXT NOT NULL,
605+
-- When the post was created
606+
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
607+
);
608+
"
609+
}
610+
481611
fn expected_single_line_comments() -> &'static [&'static str] {
482612
&[
483613
"Users table stores user account information",
@@ -532,20 +662,21 @@ mod tests {
532662
#[test]
533663
fn single_line_comment_spans_are_correct() -> Result<(), Box<dyn std::error::Error>> {
534664
use crate::{ast::ParsedSqlFileSet, files::SqlFileSet};
535-
use std::path::Path;
536-
let path = Path::new("sql_files");
537-
let set = SqlFileSet::new(path, &[])?;
665+
let base = env::temp_dir().join("single_line_spans");
666+
let _ = fs::remove_dir_all(&base);
667+
fs::create_dir_all(&base)?;
668+
let file = base.join("single.sql");
669+
fs::File::create(&file)?;
670+
fs::write(&file, single_line_comments_sql())?;
671+
let set = SqlFileSet::new(&base, &[])?;
538672
let parsed_set = ParsedSqlFileSet::parse_all(set)?;
539673
let file = parsed_set
540674
.files()
541675
.iter()
542676
.find(|f| {
543-
f.file()
544-
.path()
545-
.and_then(|p| p.to_str())
546-
.is_some_and(|p| p.ends_with("with_single_line_comments.sql"))
677+
f.file().path().and_then(|p| p.to_str()).is_some_and(|p| p.ends_with("single.sql"))
547678
})
548-
.ok_or("with_single_line_comments.sql should be present")?;
679+
.ok_or("single.sql should be present")?;
549680

550681
let comments = Comments::parse_all_comments_from_file(file)?;
551682
let comments = comments.comments();
@@ -562,27 +693,28 @@ mod tests {
562693
primary_key.span().end().column() > primary_key.span().start().column(),
563694
"end column should be after start column",
564695
);
696+
let _ = fs::remove_dir_all(&base);
565697
Ok(())
566698
}
567699

568700
#[test]
569701
fn multiline_comment_spans_are_correct() -> Result<(), Box<dyn std::error::Error>> {
570-
use std::path::Path;
571-
572702
use crate::{ast::ParsedSqlFileSet, files::SqlFileSet};
573-
let path = Path::new("sql_files");
574-
let set = SqlFileSet::new(path, &[])?;
703+
let base = env::temp_dir().join("multi_line_spans");
704+
let _ = fs::remove_dir_all(&base);
705+
fs::create_dir_all(&base)?;
706+
let file = base.join("multi.sql");
707+
fs::File::create(&file)?;
708+
fs::write(&file, multiline_comments_sql())?;
709+
let set = SqlFileSet::new(&base, &[])?;
575710
let parsed_set = ParsedSqlFileSet::parse_all(set)?;
576711
let file = parsed_set
577712
.files()
578713
.iter()
579714
.find(|f| {
580-
f.file()
581-
.path()
582-
.and_then(|p| p.to_str())
583-
.is_some_and(|p| p.ends_with("with_multiline_comments.sql"))
715+
f.file().path().and_then(|p| p.to_str()).is_some_and(|p| p.ends_with("multi.sql"))
584716
})
585-
.ok_or("with_multiline_comments.sql should be present")?;
717+
.ok_or("multi.sql should be present")?;
586718

587719
let comments = Comments::parse_all_comments_from_file(file)?;
588720
let comments = comments.comments();
@@ -606,6 +738,7 @@ mod tests {
606738
primary_key.span().end().column() > primary_key.span().start().column(),
607739
"end column should be after start column for primary key multiline comment",
608740
);
741+
let _ = fs::remove_dir_all(&base);
609742
Ok(())
610743
}
611744

0 commit comments

Comments
 (0)