Skip to content

Commit

Permalink
Check examples formatting in CI (FuelLabs#1384)
Browse files Browse the repository at this point in the history
* Make build-all-examples also check formatting.

* Install forc-fmt in CI.

* fmt examples

* Remove duplicate CI steps.

* Revert "Remove duplicate CI steps."

This reverts commit 8cbaaf1.

* Install forc-fmt prior to all build-all-examples.
  • Loading branch information
adlerjohn authored Apr 24, 2022
1 parent 87b734c commit 6a93853
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 70 deletions.
42 changes: 36 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ jobs:
with:
command: install
args: --debug --path ./forc
- name: Build Sway Examples
- name: Install Forc fmt
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
- name: Build Sway examples and check formatting
uses: actions-rs/cargo@v1
with:
command: run
Expand Down Expand Up @@ -112,7 +117,12 @@ jobs:
with:
command: install
args: --debug --path ./forc
- name: Build Sway Examples
- name: Install Forc fmt
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
- name: Build Sway examples and check formatting
uses: actions-rs/cargo@v1
with:
command: run
Expand All @@ -139,7 +149,12 @@ jobs:
with:
command: install
args: --debug --path ./forc
- name: Build Sway Examples
- name: Install Forc fmt
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
- name: Build Sway examples and check formatting
uses: actions-rs/cargo@v1
with:
command: run
Expand Down Expand Up @@ -222,7 +237,12 @@ jobs:
with:
command: install
args: --debug --path ./forc
- name: Build Sway Examples
- name: Install Forc fmt
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
- name: Build Sway examples and check formatting
uses: actions-rs/cargo@v1
with:
command: run
Expand Down Expand Up @@ -250,7 +270,12 @@ jobs:
with:
command: install
args: --debug --path ./forc
- name: Build Sway Examples
- name: Install Forc fmt
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
- name: Build Sway examples and check formatting
uses: actions-rs/cargo@v1
with:
command: run
Expand Down Expand Up @@ -297,7 +322,12 @@ jobs:
with:
command: install
args: --debug --path ./forc
- name: Build Sway Examples
- name: Install Forc fmt
uses: actions-rs/cargo@v1
with:
command: install
args: --debug --path ./forc-fmt
- name: Build Sway examples and check formatting
uses: actions-rs/cargo@v1
with:
command: run
Expand Down
22 changes: 15 additions & 7 deletions examples/build-all-examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Runs `forc build` for all projects under the Sway `examples` directory.
//! Runs `forc build` and `forc fmt --check` for all projects under the Sway `examples` directory.
//!
//! NOTE: This expects both `forc` and `cargo` to be available in `PATH`.
//! NOTE: This expects `forc`, `forc-fmt`, and `cargo` to be available in `PATH`.
use std::{
fs,
Expand All @@ -27,16 +27,24 @@ fn main() {
continue;
}

let output = std::process::Command::new("forc")
let build_output = std::process::Command::new("forc")
.args(["build", "--path"])
.arg(&path)
.output()
.expect("failed to run `forc build` for example project");

let fmt_output = std::process::Command::new("forc")
.args(["fmt", "--check", "--path"])
.arg(&path)
.output()
.expect("failed to run `forc fmt --check` for example project");

// Print output on failure so we can read it in CI.
let success = if !output.status.success() {
io::stdout().write_all(&output.stdout).unwrap();
io::stdout().write_all(&output.stderr).unwrap();
let success = if !build_output.status.success() || !fmt_output.status.success() {
io::stdout().write_all(&build_output.stdout).unwrap();
io::stdout().write_all(&fmt_output.stdout).unwrap();
io::stdout().write_all(&build_output.stderr).unwrap();
io::stdout().write_all(&fmt_output.stderr).unwrap();
false
} else {
true
Expand All @@ -45,7 +53,7 @@ fn main() {
summary.push((path, success));
}

println!("\nBuild all examples summary:");
println!("\nBuild and check formatting of all examples summary:");
let mut successes = 0;
for (path, success) in &summary {
let (checkmark, status) = if *success {
Expand Down
2 changes: 1 addition & 1 deletion examples/counter/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abi TestContract {
}

storage {
counter: u64
counter: u64,
}

impl TestContract for Contract {
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abi TestContract {
}

storage {
counter: u64
counter: u64,
}

impl TestContract for Contract {
Expand Down
16 changes: 1 addition & 15 deletions examples/liquidity_pool/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
contract;

use std::{
address::Address,
assert::assert,
context::call_frames::{
contract_id,
msg_asset_id
},
context::msg_amount,
contract_id::ContractId,
token::{
mint_to_address,
transfer_to_output
}
};
use std::{address::Address, assert::assert, context::call_frames::{contract_id, msg_asset_id}, context::msg_amount, contract_id::ContractId, token::{mint_to_address, transfer_to_output}};

abi LiquidityPool {
fn deposit(recipient: Address);
Expand Down Expand Up @@ -45,4 +32,3 @@ impl LiquidityPool for Contract {
transfer_to_output(amount_to_transfer, ~ContractId::from(BASE_TOKEN), recipient);
}
}

11 changes: 2 additions & 9 deletions examples/msg_sender/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
contract;

use std::{
address::Address,
assert::assert,
chain::auth::{AuthError, Sender, msg_sender},
panic::panic,
result::*
};
use std::{address::Address, assert::assert, chain::auth::{AuthError, Sender, msg_sender}, panic::panic, result::*};

abi MyOwnedContract {
fn receive(field_1: u64) -> bool;
}

const OWNER:b256 = 0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c;
const OWNER: b256 = 0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c;

impl MyOwnedContract for Contract {
fn receive(field_1: u64) -> bool {
Expand All @@ -26,4 +20,3 @@ impl MyOwnedContract for Contract {
true
}
}

9 changes: 1 addition & 8 deletions examples/native_token/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
contract;

use std::{
address::Address,
assert::assert,
context::*,
contract_id::ContractId,
token::*
};
use std::{address::Address, assert::assert, context::*, contract_id::ContractId, token::*};

abi NativeAssetToken {
fn mint_coins(mint_amount: u64);
Expand Down Expand Up @@ -60,4 +54,3 @@ impl NativeAssetToken for Contract {
mint_to_address(amount, recipient);
}
}

4 changes: 1 addition & 3 deletions examples/storage_example/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
contract;

use std::{
storage::{get, store}
};
use std::storage::{get, store};

abi StorageExample {
fn store_something(amount: u64);
Expand Down
10 changes: 1 addition & 9 deletions examples/subcurrency/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
// ANCHOR: body
contract;

use std::{
address::Address,
assert::assert,
chain::auth::{AuthError, Sender, msg_sender},
hash::*,
panic::panic,
result::*,
storage::{get, store}
};
use std::{address::Address, assert::assert, chain::auth::{AuthError, Sender, msg_sender}, hash::*, panic::panic, result::*, storage::{get, store}};

////////////////////////////////////////
// Event declarations
Expand Down
12 changes: 1 addition & 11 deletions examples/wallet_smart_contract/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
contract;

use std::{
address::Address,
assert::assert,
chain::auth::{AuthError, Sender, msg_sender},
constants::NATIVE_ASSET_ID,
context::{call_frames::msg_asset_id, msg_amount},
contract_id::ContractId,
panic::panic,
result::*,
token::transfer_to_output
};
use std::{address::Address, assert::assert, chain::auth::{AuthError, Sender, msg_sender}, constants::NATIVE_ASSET_ID, context::{call_frames::msg_asset_id, msg_amount}, contract_id::ContractId, panic::panic, result::*, token::transfer_to_output};

const OWNER_ADDRESS: b256 = 0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861;

Expand Down

0 comments on commit 6a93853

Please sign in to comment.