Skip to content

Commit

Permalink
Merge pull request #89 from noritada/feat/readme-testing
Browse files Browse the repository at this point in the history
Make README.md up to date and testable

This PR makes example code and CLI's help message in README.md up to date.
Also, since the library API and CLI's help message can change in future versions,
This PR makes their descriptions in README.md testable so that we can know the need for changes.
  • Loading branch information
noritada committed Jul 2, 2024
2 parents b5371cd + c220a23 commit c567460
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ If you feel a feature is missing, please send us your suggestions through the [G
### Usage example

```rust
use grib::{self, codetables::grib2::*, ForecastTime, Grib2SubmessageDecoder, Name};
use grib::{codetables::grib2::*, ForecastTime, Grib2SubmessageDecoder, Name};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let fname = "Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin";
let fname = "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin";
let f = std::fs::File::open(fname)?;
let f = std::io::BufReader::new(f);
let grib2 = grib::from_reader(f)?;
Expand Down Expand Up @@ -130,12 +130,12 @@ The [examples directory](examples) may help you understand the API.

CLI application `gribber` built on the top of the `grib` library is available. It is in the `grib-cli` package and can be installed via `cargo install grib-cli`.

```
```text
Usage: gribber [COMMAND]
Commands:
completions Generate shell completions for your shell to stdout
decode Export decoded data
decode Export decoded data with latitudes and longitudes
info Show identification information
inspect Inspect and describes the data structure
list List layers contained in the data
Expand All @@ -159,7 +159,7 @@ This repository uses the submodules functionality of Git. So, before running `ca

Then you can build it in the usual way in the Rust world.

```
```shell
cargo build
```

Expand Down
17 changes: 17 additions & 0 deletions cli/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ fn help() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
fn readme_consistent_with_help_message() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin(CMD_NAME)?;
cmd.arg("--help");
let help_msg = cmd.output()?.stdout;
let help_msg = String::from_utf8(help_msg)?;
#[cfg(target_os = "windows")]
let help_msg = help_msg.replace(&format!("{}.exe", CMD_NAME), CMD_NAME);

let readme = include_str!("../../../README.md");
#[cfg(target_os = "windows")]
let readme = readme.replace("\r\n", "\n");
assert!(readme.contains(&help_msg));

Ok(())
}

#[test]
fn no_subcommand_specified() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin(CMD_NAME)?;
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/cli/utils/testdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ pub(crate) mod grib2 {
}

pub(crate) fn jma_tornado_nowcast() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(
cat_to_tempfile(
testdata_dir()
.join("Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin.xz"),
.join("Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin"),
)
}

Expand Down
4 changes: 2 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,14 +781,14 @@ mod tests {
test_submessage_iterator! {
(
item_0_from_submessage_iterator_for_single_message_data_with_multiple_submessages,
"testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin.xz",
"testdata/Z__C_RJTD_20190304000000_MSM_GUID_Rjp_P-all_FH03-39_Toorg_grib2.bin.xz",
0,
(0, 0),
(0, 1, None, 2, 3, 4, 5, 6, 0),
),
(
item_1_from_submessage_iterator_for_single_message_data_with_multiple_submessages,
"testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin.xz",
"testdata/Z__C_RJTD_20190304000000_MSM_GUID_Rjp_P-all_FH03-39_Toorg_grib2.bin.xz",
1,
(0, 1),
(0, 1, None, 2, 7, 8, 9, 10, 0),
Expand Down
14 changes: 12 additions & 2 deletions src/grid/earth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ mod tests {
Ok(buf)
}

fn cat_as_bytes(file_name: &str) -> Result<Vec<u8>, std::io::Error> {
let mut buf = Vec::new();

let f = File::open(file_name)?;
let mut f = BufReader::new(f);
f.read_to_end(&mut buf)?;

Ok(buf)
}

#[test]
fn radii_for_shape_1() -> Result<(), Box<dyn std::error::Error>> {
let buf = unxz_as_bytes("testdata/ds.critfireo.bin.xz")?;
Expand Down Expand Up @@ -145,8 +155,8 @@ mod tests {

#[test]
fn radii_for_shape_4() -> Result<(), Box<dyn std::error::Error>> {
let buf = unxz_as_bytes(
"testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin.xz",
let buf = cat_as_bytes(
"testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin",
)?;
let earth_actual = EarthShapeDefinition::from_buf(&buf[0x33..]);
let earth_expected = EarthShapeDefinition {
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ pub use crate::{
parser::*,
reader::*,
};

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;
Binary file not shown.
Binary file not shown.

0 comments on commit c567460

Please sign in to comment.