Skip to content

Commit

Permalink
df: use blocksize of 512 if POSIXLY_CORRECT is set
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed May 6, 2022
1 parent a711597 commit d8a8ddd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/uu/df/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! Types for representing and displaying block sizes.
use crate::OPT_BLOCKSIZE;
use clap::ArgMatches;
use std::fmt;
use std::{env, fmt};

use uucore::parse_size::{parse_size, ParseSizeError};

Expand Down Expand Up @@ -159,6 +159,7 @@ pub(crate) enum HumanReadable {
/// size.
///
/// The default variant is `Bytes(1024)`.
#[derive(Debug, PartialEq)]
pub(crate) enum BlockSize {
/// A fixed number of bytes.
///
Expand All @@ -168,7 +169,11 @@ pub(crate) enum BlockSize {

impl Default for BlockSize {
fn default() -> Self {
Self::Bytes(1024)
if env::var("POSIXLY_CORRECT").is_ok() {
Self::Bytes(512)
} else {
Self::Bytes(1024)
}
}
}

Expand All @@ -195,6 +200,8 @@ impl fmt::Display for BlockSize {
#[cfg(test)]
mod tests {

use std::env;

use crate::blocks::{to_magnitude_and_suffix, BlockSize};

#[test]
Expand Down Expand Up @@ -245,4 +252,12 @@ mod tests {
assert_eq!(format!("{}", BlockSize::Bytes(2 * 1024)), "2K");
assert_eq!(format!("{}", BlockSize::Bytes(3 * 1024 * 1024)), "3M");
}

#[test]
fn test_default_block_size() {
assert_eq!(BlockSize::Bytes(1024), BlockSize::default());
env::set_var("POSIXLY_CORRECT", "1");
assert_eq!(BlockSize::Bytes(512), BlockSize::default());
env::remove_var("POSIXLY_CORRECT");
}
}
20 changes: 20 additions & 0 deletions tests/by-util/test_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,26 @@ fn test_iuse_percentage() {
}
}

#[test]
fn test_default_block_size() {
let output = new_ucmd!()
.arg("--output=size")
.succeeds()
.stdout_move_str();
let header = output.lines().next().unwrap().to_string();

assert_eq!(header, "1K-blocks");

let output = new_ucmd!()
.arg("--output=size")
.env("POSIXLY_CORRECT", "1")
.succeeds()
.stdout_move_str();
let header = output.lines().next().unwrap().to_string();

assert_eq!(header, "512B-blocks");
}

#[test]
fn test_block_size_1024() {
fn get_header(block_size: u64) -> String {
Expand Down

0 comments on commit d8a8ddd

Please sign in to comment.