Skip to content

Commit 19143be

Browse files
committed
test(gencat): add tests for gencat sets and messages
1 parent 7c11da0 commit 19143be

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

i18n/gencat.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use bytemuck::NoUninit;
21
use byteorder::{BigEndian, ByteOrder, LittleEndian, NativeEndian, WriteBytesExt};
32
use clap::Parser;
43
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
@@ -8,7 +7,7 @@ use std::{
87
collections::BTreeMap,
98
fmt::Display,
109
fs::File,
11-
io::{self, Cursor, Read, Seek, SeekFrom, Write},
10+
io::{self, Cursor, Read, Seek, Write},
1211
num::ParseIntError,
1312
path::PathBuf,
1413
rc::Rc,
@@ -18,11 +17,15 @@ const NL_SETMAX: u32 = 255; //max set number(the limits.h defines it and is ment
1817
const NL_SETD: u32 = 1; // the default set number for the messages that are not in any set
1918
const GLIBC_MAGIC: u32 = 0x960408de;
2019

21-
const OSX_MAGIC: &[u8; 8] = b"*nazgul*";
22-
const OSX_MAJOR_VER: i32 = 1;
23-
const OSX_MINOR_VER: i32 = 0;
24-
const OSX_BYTE_ORDER: i32 = 0x01; // denotes BIG ENDIAN for now
25-
const OSX_NOT_INVALID_FLAG: i32 = 0;
20+
#[cfg(target_os = "macos")]
21+
pub mod osx {
22+
pub const OSX_MAGIC: &[u8; 8] = b"*nazgul*";
23+
pub const OSX_MAJOR_VER: i32 = 1;
24+
pub const OSX_MINOR_VER: i32 = 0;
25+
pub const OSX_BYTE_ORDER: i32 = 0x01; // denotes BIG ENDIAN for now
26+
pub const OSX_NOT_INVALID_FLAG: i32 = 0;
27+
pub const FIRST_SET_OFFSET: i64 = 32;
28+
}
2629

2730
/// gencat - generate a formatted message catalog
2831
#[derive(Parser, Debug)]
@@ -78,6 +81,7 @@ pub struct Set {
7881
}
7982

8083
impl Set {
84+
#[cfg(target_os = "macos")]
8185
fn get_msgs_count(&self) -> i32 {
8286
let mut count = 0;
8387
let mut current = self.first_msg.clone();
@@ -180,7 +184,8 @@ pub struct MessageCatalog {
180184
}
181185

182186
/// Magic Header structure for the catalog file
183-
#[derive(NoUninit, Clone, Copy)]
187+
#[cfg(target_os = "macos")]
188+
#[derive(bytemuck::NoUninit, Clone, Copy)]
184189
#[repr(C)]
185190
struct CatFileMagicHeader {
186191
/// Magic cookie "*nazgul*"
@@ -261,6 +266,7 @@ impl MessageCatalog {
261266
},
262267
};
263268

269+
#[cfg(not(target_os = "macos"))]
264270
if build_default {
265271
catalog.add_set(NL_SETD, String::from("Default Set"));
266272
}
@@ -603,9 +609,9 @@ impl MessageCatalog {
603609
Ok(catalog)
604610
}
605611

606-
/// Write to the cat file **GNU compatible(anything not macos for now)**
607-
//#[cfg(not(target_os = "macos"))]
608-
pub fn write_catfile_another<T: Write + Seek>(
612+
/// Write to the cat file **for GNU only**
613+
#[cfg(not(target_os = "macos"))]
614+
pub fn write_catfile<T: Write + Seek>(
609615
&self,
610616
file: &mut T,
611617
) -> Result<(), Box<dyn std::error::Error>> {
@@ -638,19 +644,19 @@ impl MessageCatalog {
638644
}
639645

640646
/// Write to the cat file **for OSX only**
641-
//#[cfg(target_os = "macos")]
647+
#[cfg(target_os = "macos")]
642648
pub fn write_catfile<T: Write + Seek>(
643649
&self,
644650
file: &mut T,
645651
) -> Result<(), Box<dyn std::error::Error>> {
646652
let header = CatFileMagicHeader {
647-
magic: *OSX_MAGIC,
648-
major_ver: OSX_MAJOR_VER.to_be(),
649-
minor_ver: OSX_MINOR_VER.to_be(),
650-
flags: OSX_BYTE_ORDER.to_be(),
653+
magic: *osx::OSX_MAGIC,
654+
major_ver: osx::OSX_MAJOR_VER.to_be(),
655+
minor_ver: osx::OSX_MINOR_VER.to_be(),
656+
flags: osx::OSX_BYTE_ORDER.to_be(),
651657
num_sets: self.cat.total_sets().to_be(),
652-
// for now we have set it to 0, but we will change this later on as we lay the first set details
653-
first_set: 0,
658+
// for now we have set it to 32, but we will change this later on as we lay the first set details
659+
first_set: osx::FIRST_SET_OFFSET.to_be(),
654660
};
655661

656662
file.write_all(bytemuck::bytes_of(&header))?;
@@ -667,9 +673,9 @@ impl MessageCatalog {
667673
let set_pos = file.stream_position()?;
668674

669675
if set.set_id == 1 {
670-
file.seek(SeekFrom::Start(first_set_pos))?;
676+
file.seek(io::SeekFrom::Start(first_set_pos))?;
671677
file.write_u64::<BigEndian>(set_pos)?;
672-
file.seek(SeekFrom::Start(set_pos))?;
678+
file.seek(io::SeekFrom::Start(set_pos))?;
673679
}
674680

675681
file.write_u32::<BigEndian>(set.set_id)?;
@@ -688,7 +694,7 @@ impl MessageCatalog {
688694

689695
let num_msgs = set.get_msgs_count();
690696
file.write_i32::<BigEndian>(num_msgs)?;
691-
file.write_i32::<BigEndian>(OSX_NOT_INVALID_FLAG)?;
697+
file.write_i32::<BigEndian>(osx::OSX_NOT_INVALID_FLAG)?;
692698

693699
// We'll write the string data now
694700
let data_offset = file.stream_position()?;
@@ -714,23 +720,23 @@ impl MessageCatalog {
714720

715721
file.write_i32::<BigEndian>(msg.msg_id as i32)?;
716722
file.write_i64::<BigEndian>(msg.offset)?;
717-
file.write_i32::<BigEndian>(OSX_NOT_INVALID_FLAG)?;
723+
file.write_i32::<BigEndian>(osx::OSX_NOT_INVALID_FLAG)?;
718724

719725
current_msg = msg.next.clone();
720726
}
721727

722728
let current_pos = file.stream_position()?;
723729

724730
// go back and write first msg offset
725-
file.seek(SeekFrom::Start(first_msg_offset_pos))?;
731+
file.seek(io::SeekFrom::Start(first_msg_offset_pos))?;
726732
file.write_i64::<BigEndian>(first_msg_offset as i64)?;
727733

728734
// go back and write data offset
729-
file.seek(SeekFrom::Start(data_offset_pos))?;
735+
file.seek(io::SeekFrom::Start(data_offset_pos))?;
730736
file.write_i64::<BigEndian>(data_offset as i64)?;
731737

732738
// go back and write data length
733-
file.seek(SeekFrom::Start(data_length_pos))?;
739+
file.seek(io::SeekFrom::Start(data_length_pos))?;
734740
file.write_i32::<BigEndian>(data_length)?;
735741

736742
let last_set = cat.last_set.as_ref().unwrap();
@@ -739,11 +745,11 @@ impl MessageCatalog {
739745
// if not last then we need to write the next set offset
740746
if set.set_id != last_set.set_id {
741747
// go back and write next set offset
742-
file.seek(SeekFrom::Start(next_set_offset))?;
748+
file.seek(io::SeekFrom::Start(next_set_offset))?;
743749
file.write_i64::<BigEndian>(current_pos as i64)?;
744750
}
745751

746-
file.seek(SeekFrom::Start(current_pos))?;
752+
file.seek(io::SeekFrom::Start(current_pos))?;
747753

748754
current_set = set.next.clone();
749755
}

i18n/tests/gencat/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,29 @@ fn gencat_empty_message_file() {
5050
Vec::new(),
5151
);
5252
}
53+
54+
#[test]
55+
fn gencat_sets_and_messagess() {
56+
let cargo_manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
57+
let msg_file = cargo_manifest_dir.join("tests/gencat/sets_and_messages.msg");
58+
59+
#[cfg(not(target_os = "macos"))]
60+
let expected_cat_file =
61+
cargo_manifest_dir.join("tests/gencat/sets_and_messages_gnu_catfile.cat");
62+
63+
#[cfg(target_os = "macos")]
64+
let expected_cat_file =
65+
cargo_manifest_dir.join("tests/gencat/sets_and_messages_osx_catfile.cat");
66+
67+
let mut expected_output: Vec<u8> = Vec::new();
68+
File::open(&expected_cat_file)
69+
.unwrap()
70+
.read_to_end(&mut expected_output)
71+
.unwrap();
72+
73+
gencat_test(
74+
&["-", msg_file.to_str().unwrap()],
75+
expected_output,
76+
Vec::new(),
77+
);
78+
}

0 commit comments

Comments
 (0)