Skip to content

Commit d5cafeb

Browse files
authored
Add workflow that tests and checks (#14)
1 parent 33303d0 commit d5cafeb

File tree

17 files changed

+81
-44
lines changed

17 files changed

+81
-44
lines changed

.github/workflows/CI.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ permissions:
1919
pull_request: null
2020
workflow_dispatch: null
2121
jobs:
22+
test:
23+
uses: ./.github/workflows/test.yml
2224
build-napi:
25+
needs: test
2326
strategy:
2427
fail-fast: false
2528
matrix:
@@ -133,6 +136,7 @@ jobs:
133136
if-no-files-found: error
134137

135138
build-wasm:
139+
needs: test
136140
runs-on: ubuntu-latest
137141
permissions:
138142
contents: write

.github/workflows/test.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Test and Check
2+
env:
3+
TEST_FILES_DST_DIR: crates/uroborosql-fmt/testfiles/dst
4+
on:
5+
workflow_call: null
6+
workflow_dispatch: null
7+
jobs:
8+
test-and-check:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
13+
- name: Setup cargo
14+
uses: dtolnay/rust-toolchain@stable
15+
with:
16+
toolchain: stable
17+
components: rustfmt, clippy
18+
19+
- name: Check error and warning
20+
run: |
21+
result=$(cargo check --quiet 2>&1)
22+
echo ${result}
23+
test -z ${result}
24+
25+
- name: Fmt
26+
run: cargo fmt --check
27+
28+
- name: Clippy
29+
run: cargo clippy -- --deny warnings
30+
31+
- name: Execute test
32+
run: cargo test --workspace
33+
34+
- name: Check test files diff
35+
run: git diff --exit-code

crates/uroborosql-fmt-cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn main() {
2424
let result = match format_sql(src.as_ref(), config_path) {
2525
Ok(res) => res,
2626
Err(e) => {
27-
eprintln!("{}", e);
27+
eprintln!("{e}");
2828
src
2929
}
3030
};
@@ -34,6 +34,6 @@ fn main() {
3434
let mut file = File::create(path).unwrap();
3535
file.write_all(result.as_bytes()).unwrap();
3636
}
37-
None => println!("{}", result),
37+
None => println!("{result}"),
3838
}
3939
}

crates/uroborosql-fmt-napi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ pub fn runfmt(input: String, config_path: Option<&str>) -> Result<String> {
1212

1313
match result {
1414
Ok(res) => Ok(res),
15-
Err(e) => Err(Error::new(Status::GenericFailure, format!("{}", e))),
15+
Err(e) => Err(Error::new(Status::GenericFailure, format!("{e}"))),
1616
}
1717
}

crates/uroborosql-fmt-wasm/src/lib.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ use std::ffi::{c_char, CStr, CString};
22

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

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

13-
let config_json_str = unsafe { CStr::from_ptr(config_json_str).to_str().unwrap() };
19+
let config_json_str = CStr::from_ptr(config_json_str).to_str().unwrap();
1420
let config = Config::from_json_str(config_json_str).unwrap();
1521

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

28+
/// Free the string `s` allocated by Rust.
29+
///
30+
/// # Safety
31+
///
32+
/// This is unsafe because it uses the unsafe function
33+
/// [`CString::from_war()`](https://doc.rust-lang.org/std/ffi/struct.CString.html#method.from_raw).
2234
#[no_mangle]
23-
pub extern "C" fn free_format_string(s: *mut c_char) {
24-
unsafe {
25-
if s.is_null() {
26-
return;
27-
}
28-
CString::from_raw(s)
29-
};
35+
pub unsafe extern "C" fn free_format_string(s: *mut c_char) {
36+
if s.is_null() {
37+
return;
38+
}
39+
let _ = CString::from_raw(s);
3040
}

crates/uroborosql-fmt/src/cst.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Comment {
177177
.unwrap_or(0);
178178

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

182182
// 各行を描画
183183
for line in &lines {

crates/uroborosql-fmt/src/cst/body/with.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ impl Cte {
5555
if comment.is_block_comment() {
5656
// 複数行コメント
5757
Err(UroboroSQLFmtError::IllegalOperation(format!(
58-
"set_trailing_comment:{:?} is not trailing comment!",
59-
comment
58+
"set_trailing_comment:{comment:?} is not trailing comment!"
6059
)))
6160
} else {
6261
let Comment { text, loc } = comment;
@@ -80,8 +79,7 @@ impl Cte {
8079
if comment.is_block_comment() {
8180
// 複数行コメント
8281
Err(UroboroSQLFmtError::IllegalOperation(format!(
83-
"set_name_trailing_comment:{:?} is not trailing comment!",
84-
comment
82+
"set_name_trailing_comment:{comment:?} is not trailing comment!"
8583
)))
8684
} else {
8785
// 行コメント

crates/uroborosql-fmt/src/cst/expr.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,13 @@ impl Expr {
142142
aligned.set_trailing_comment(comment)?;
143143
} else {
144144
return Err(UroboroSQLFmtError::Unimplemented(format!(
145-
"add_comment_to_child(): this comment is not trailing comment\nexpr: {:?}comment: {:?}\n",
146-
aligned,
147-
comment
145+
"add_comment_to_child(): this comment is not trailing comment\nexpr: {aligned:?}comment: {comment:?}\n"
148146
)));
149147
}
150148
}
151149
Expr::Primary(primary) => {
152150
return Err(UroboroSQLFmtError::Unimplemented(format!(
153-
"add_comment_to_child(): unimplemented for primary\nexpr: {:?}",
154-
primary
151+
"add_comment_to_child(): unimplemented for primary\nexpr: {primary:?}"
155152
)));
156153
}
157154

@@ -166,8 +163,7 @@ impl Expr {
166163

167164
Expr::Cond(cond) => {
168165
return Err(UroboroSQLFmtError::Unimplemented(format!(
169-
"add_comment_to_child(): unimplemented for conditional_expr\nexpr: {:?}",
170-
cond
166+
"add_comment_to_child(): unimplemented for conditional_expr\nexpr: {cond:?}"
171167
)));
172168
}
173169
_ => {

crates/uroborosql-fmt/src/cst/expr/aligned.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ impl AlignedExpr {
149149
if comment.is_block_comment() {
150150
// 複数行コメント
151151
Err(UroboroSQLFmtError::IllegalOperation(format!(
152-
"set_trailing_comment:{:?} is not trailing comment!",
153-
comment
152+
"set_trailing_comment:{comment:?} is not trailing comment!"
154153
)))
155154
} else {
156155
let Comment { text, loc } = comment;
@@ -174,8 +173,7 @@ impl AlignedExpr {
174173
if comment.is_block_comment() {
175174
// 複数行コメント
176175
Err(UroboroSQLFmtError::IllegalOperation(format!(
177-
"set_lhs_trailing_comment:{:?} is not trailing comment!",
178-
comment
176+
"set_lhs_trailing_comment:{comment:?} is not trailing comment!"
179177
)))
180178
} else {
181179
// 行コメント

crates/uroborosql-fmt/src/cst/expr/function.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ impl FunctionCallArgs {
6767
last.set_trailing_comment(comment)
6868
} else {
6969
Err(UroboroSQLFmtError::IllegalOperation(format!(
70-
"set_trailing_comment:{:?} is not trailing comment!",
71-
comment
70+
"set_trailing_comment:{comment:?} is not trailing comment!"
7271
)))
7372
}
7473
}

0 commit comments

Comments
 (0)