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

Add workflow that tests and checks #14

Merged
merged 5 commits into from
Oct 11, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ permissions:
pull_request: null
workflow_dispatch: null
jobs:
test:
uses: ./.github/workflows/test.yml
build-napi:
needs: test
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -133,6 +136,7 @@ jobs:
if-no-files-found: error

build-wasm:
needs: test
runs-on: ubuntu-latest
permissions:
contents: write
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Test and Check
env:
TEST_FILES_DST_DIR: crates/uroborosql-fmt/testfiles/dst
on:
workflow_call: null
workflow_dispatch: null
jobs:
test-and-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup cargo
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: rustfmt, clippy

- name: Check error and warning
run: |
result=$(cargo check --quiet 2>&1)
echo ${result}
test -z ${result}

- name: Fmt
run: cargo fmt --check

- name: Clippy
run: cargo clippy -- --deny warnings

- name: Execute test
run: cargo test --workspace

- name: Check test files diff
run: git diff --exit-code
4 changes: 2 additions & 2 deletions crates/uroborosql-fmt-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
let result = match format_sql(src.as_ref(), config_path) {
Ok(res) => res,
Err(e) => {
eprintln!("{}", e);
eprintln!("{e}");
src
}
};
Expand All @@ -34,6 +34,6 @@ fn main() {
let mut file = File::create(path).unwrap();
file.write_all(result.as_bytes()).unwrap();
}
None => println!("{}", result),
None => println!("{result}"),
}
}
2 changes: 1 addition & 1 deletion crates/uroborosql-fmt-napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ pub fn runfmt(input: String, config_path: Option<&str>) -> Result<String> {

match result {
Ok(res) => Ok(res),
Err(e) => Err(Error::new(Status::GenericFailure, format!("{}", e))),
Err(e) => Err(Error::new(Status::GenericFailure, format!("{e}"))),
}
}
30 changes: 20 additions & 10 deletions crates/uroborosql-fmt-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ use std::ffi::{c_char, CStr, CString};

use uroborosql_fmt::{config::Config, format_sql_with_config};

/// Formats SQL code given as char pointer `src` by WASM (JavaScript).
///
/// # Safety
///
/// This is unsafe because it uses unsafe function
/// [`CStr::from_ptr`](https://doc.rust-lang.org/stable/std/ffi/struct.CStr.html#method.from_ptr).
#[export_name = "format_sql"]
#[no_mangle]
pub extern "C" fn format_sql_for_wasm(
pub unsafe extern "C" fn format_sql_for_wasm(
src: *mut c_char,
config_json_str: *mut c_char,
) -> *mut c_char {
let src = unsafe { CStr::from_ptr(src).to_str().unwrap().to_owned() };
let src = CStr::from_ptr(src).to_str().unwrap().to_owned();

let config_json_str = unsafe { CStr::from_ptr(config_json_str).to_str().unwrap() };
let config_json_str = CStr::from_ptr(config_json_str).to_str().unwrap();
let config = Config::from_json_str(config_json_str).unwrap();

// TODO: error handling
Expand All @@ -19,12 +25,16 @@ pub extern "C" fn format_sql_for_wasm(
CString::new(result).unwrap().into_raw()
}

/// Free the string `s` allocated by Rust.
///
/// # Safety
///
/// This is unsafe because it uses the unsafe function
/// [`CString::from_war()`](https://doc.rust-lang.org/std/ffi/struct.CString.html#method.from_raw).
#[no_mangle]
pub extern "C" fn free_format_string(s: *mut c_char) {
unsafe {
if s.is_null() {
return;
}
CString::from_raw(s)
};
pub unsafe extern "C" fn free_format_string(s: *mut c_char) {
if s.is_null() {
return;
}
let _ = CString::from_raw(s);
}
2 changes: 1 addition & 1 deletion crates/uroborosql-fmt/src/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Comment {
.unwrap_or(0);

// 開始キーワードを描画して改行
result.push_str(&format!("{}\n", start_keyword));
result.push_str(&format!("{start_keyword}\n"));

// 各行を描画
for line in &lines {
Expand Down
6 changes: 2 additions & 4 deletions crates/uroborosql-fmt/src/cst/body/with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ impl Cte {
if comment.is_block_comment() {
// 複数行コメント
Err(UroboroSQLFmtError::IllegalOperation(format!(
"set_trailing_comment:{:?} is not trailing comment!",
comment
"set_trailing_comment:{comment:?} is not trailing comment!"
)))
} else {
let Comment { text, loc } = comment;
Expand All @@ -80,8 +79,7 @@ impl Cte {
if comment.is_block_comment() {
// 複数行コメント
Err(UroboroSQLFmtError::IllegalOperation(format!(
"set_name_trailing_comment:{:?} is not trailing comment!",
comment
"set_name_trailing_comment:{comment:?} is not trailing comment!"
)))
} else {
// 行コメント
Expand Down
10 changes: 3 additions & 7 deletions crates/uroborosql-fmt/src/cst/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,13 @@ impl Expr {
aligned.set_trailing_comment(comment)?;
} else {
return Err(UroboroSQLFmtError::Unimplemented(format!(
"add_comment_to_child(): this comment is not trailing comment\nexpr: {:?}comment: {:?}\n",
aligned,
comment
"add_comment_to_child(): this comment is not trailing comment\nexpr: {aligned:?}comment: {comment:?}\n"
)));
}
}
Expr::Primary(primary) => {
return Err(UroboroSQLFmtError::Unimplemented(format!(
"add_comment_to_child(): unimplemented for primary\nexpr: {:?}",
primary
"add_comment_to_child(): unimplemented for primary\nexpr: {primary:?}"
)));
}

Expand All @@ -166,8 +163,7 @@ impl Expr {

Expr::Cond(cond) => {
return Err(UroboroSQLFmtError::Unimplemented(format!(
"add_comment_to_child(): unimplemented for conditional_expr\nexpr: {:?}",
cond
"add_comment_to_child(): unimplemented for conditional_expr\nexpr: {cond:?}"
)));
}
_ => {
Expand Down
6 changes: 2 additions & 4 deletions crates/uroborosql-fmt/src/cst/expr/aligned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ impl AlignedExpr {
if comment.is_block_comment() {
// 複数行コメント
Err(UroboroSQLFmtError::IllegalOperation(format!(
"set_trailing_comment:{:?} is not trailing comment!",
comment
"set_trailing_comment:{comment:?} is not trailing comment!"
)))
} else {
let Comment { text, loc } = comment;
Expand All @@ -174,8 +173,7 @@ impl AlignedExpr {
if comment.is_block_comment() {
// 複数行コメント
Err(UroboroSQLFmtError::IllegalOperation(format!(
"set_lhs_trailing_comment:{:?} is not trailing comment!",
comment
"set_lhs_trailing_comment:{comment:?} is not trailing comment!"
)))
} else {
// 行コメント
Expand Down
3 changes: 1 addition & 2 deletions crates/uroborosql-fmt/src/cst/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ impl FunctionCallArgs {
last.set_trailing_comment(comment)
} else {
Err(UroboroSQLFmtError::IllegalOperation(format!(
"set_trailing_comment:{:?} is not trailing comment!",
comment
"set_trailing_comment:{comment:?} is not trailing comment!"
)))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/uroborosql-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) fn format(src: &str, language: Language) -> Result<String, UroboroSQL
let stmts = visitor.visit_sql(root_node, src.as_ref())?;

if CONFIG.read().unwrap().debug {
eprintln!("{:#?}", stmts);
eprintln!("{stmts:#?}");
}

let result = stmts
Expand Down
3 changes: 1 addition & 2 deletions crates/uroborosql-fmt/src/re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ pub(crate) static RE: Lazy<Re> = Lazy::new(|| Re {
begin_re: Regex::new(BEGIN_PATTERN).unwrap(),
branching_keyword_re: Regex::new(
format!(
"({})|({})|({})|({})|({})",
IF_PATTERN, ELIF_PATTERN, ELSE_PATTERN, END_PATTERN, BEGIN_PATTERN
"({IF_PATTERN})|({ELIF_PATTERN})|({ELSE_PATTERN})|({END_PATTERN})|({BEGIN_PATTERN})"
)
.as_str(),
)
Expand Down
2 changes: 1 addition & 1 deletion crates/uroborosql-fmt/src/two_way_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) fn format_two_way_sql(
eprintln!("{}", "-".repeat(100));

for source in formatted_tree.to_vec_string() {
eprintln!("{}", source);
eprintln!("{source}");
eprintln!("{}", "-".repeat(100));
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/uroborosql-fmt/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn compare_tokens(
} else {
return Err(UroboroSQLFmtError::Validation {
format_result: format_result.to_owned(),
error_msg: format!("different kind token: src={:?}, dst={:?}", src_tok, dst_tok),
error_msg: format!("different kind token: src={src_tok:?}, dst={dst_tok:?}"),
});
}
}
Expand All @@ -153,8 +153,7 @@ fn compare_token_text(
Err(UroboroSQLFmtError::Validation {
format_result: format_result.to_owned(),
error_msg: format!(
r#"hint must start with "/*+" or "--+". src={:?}, dst={:?}"#,
src_tok, dst_tok
r#"hint must start with "/*+" or "--+". src={src_tok:?}, dst={dst_tok:?}"#
),
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/uroborosql-fmt/src/visitor/clause/with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Visitor {

if let Some(materialized_keyword) = &mut materialized_keyword {
// NOTがある場合にしかこの分岐に入らないのでMATERIALIZEDの前に空白を付与して挿入する
materialized_keyword.push_str(&format!(" {}", materialized));
materialized_keyword.push_str(&format!(" {materialized}"));
} else {
// NOTがない場合
materialized_keyword = Some(materialized);
Expand Down
3 changes: 1 addition & 2 deletions crates/uroborosql-fmt/src/visitor/expr/in_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ impl Visitor {
column_list.set_head_comment(comment);
} else {
return Err(UroboroSQLFmtError::UnexpectedSyntax(format!(
"visit_in_expr(): unexpected comment\n{:?}",
comment
"visit_in_expr(): unexpected comment\n{comment:?}"
)));
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/uroborosql-fmt/tests/test_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn test_all_files() -> bool {
eprintln!("-- test_all_files out --");
eprintln!("failed files ...");
failure_results.iter().for_each(|(path, error_msg)| {
eprintln!("{}: {}", path, error_msg);
eprintln!("{path}: {error_msg}");
});
eprintln!("{} files failed", failure_results.len());
return false;
Expand Down Expand Up @@ -165,7 +165,7 @@ fn test_config_file() -> bool {
eprintln!("-- test_config_file out --");
eprintln!("failed files ...");
failure_results.iter().for_each(|(path, error_msg)| {
eprintln!("{}: {}", path, error_msg);
eprintln!("{path}: {error_msg}");
});
eprintln!("{} files failed", failure_results.len());
return false;
Expand All @@ -186,7 +186,7 @@ fn test_entry_with_config(

// dstディレクトリに、対応するディレクトリを生成
if let Err(e) = create_dir_all(path::Path::new(&directory_path)) {
panic!("create_dir: {:?}", e)
panic!("create_dir: {e:?}")
}

let entries = src_path.read_dir().unwrap();
Expand Down