| // Copyright 2020 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| use anyhow::{ensure, Error}; |
| use pretty_assertions::assert_eq; |
| use regex::Regex; |
| use std::env; |
| use std::path::PathBuf; |
| use std::process::Command; |
| |
| fn test_path() -> PathBuf { |
| let mut path = env::current_exe().unwrap(); |
| path.pop(); |
| if !path.join("test_data").exists() { |
| // We're inside exe.unstripped. |
| path.pop(); |
| } |
| path |
| } |
| |
| fn resolve_path(suffix: &str) -> Result<String, Error> { |
| let mut path = test_path().join("exe.unstripped").join(suffix); |
| if !path.exists() { |
| path = test_path().join(suffix); |
| } |
| ensure!(path.exists(), "Not found: {:?}", &path); |
| Ok(path.to_str().unwrap().to_string()) |
| } |
| |
| fn assert_valid_tz_version(tz_version: &str) -> Result<(), Error> { |
| // Note: Standard TZ versions are of the form "2019a", but as of 2021-09, in ICU there can also |
| // be "in-between" versions with additional suffixes. See, for example, |
| // https://github.com/unicode-org/icu/pull/1885/. |
| let re = Regex::new(r"^20[0-9][0-9][a-z][a-z0-9]*$")?; |
| assert!( |
| re.is_match(tz_version), |
| "Actual tz version '{}' did not match /{:?}/", |
| &tz_version, |
| &re |
| ); |
| Ok(()) |
| } |
| |
| #[test] |
| fn test_extract_tz_version_no_tzres() -> Result<(), Error> { |
| let mut cmd = Command::new(resolve_path("icu_data_extractor")?); |
| cmd.arg(format!( |
| "--icu-data-file={}", |
| resolve_path("test_data/icu_data_extractor/icudtl.dat")? |
| )) |
| .arg("tz-version"); |
| |
| let output = cmd.output()?; |
| assert!(output.status.success()); |
| |
| let tz_version = std::str::from_utf8(&output.stdout)?; |
| assert_valid_tz_version(&tz_version) |
| } |
| |
| #[test] |
| fn test_extract_tz_version_tzres() -> Result<(), Error> { |
| let mut cmd = Command::new(resolve_path("icu_data_extractor")?); |
| cmd.arg(format!( |
| "--icu-data-file={}", |
| resolve_path("test_data/icu_data_extractor/icudtl.dat")? |
| )) |
| .arg(format!("--tz-res-dir={}", resolve_path("test_data/icu_data_extractor/tzres")?)) |
| .arg("tz-version"); |
| |
| let output = cmd.output()?; |
| assert!(output.status.success()); |
| |
| let tz_version = std::str::from_utf8(&output.stdout)?; |
| assert_valid_tz_version(&tz_version) |
| } |
| |
| #[test] |
| fn test_extract_tz_ids() -> Result<(), Error> { |
| let mut cmd = Command::new(resolve_path("icu_data_extractor")?); |
| cmd.arg(format!( |
| "--icu-data-file={}", |
| resolve_path("test_data/icu_data_extractor/icudtl.dat")? |
| )) |
| .arg("tz-ids") |
| .arg("--fixed-order=America/Los_Angeles,Europe/Paris,AET") |
| .arg("--delimiter=,"); |
| |
| let output = cmd.output()?; |
| assert!(output.status.success()); |
| |
| let tz_ids = std::str::from_utf8(&output.stdout)?; |
| let expected = "America/Los_Angeles,Europe/Paris,AET,ACT,AGT"; |
| assert_eq!(&tz_ids[0..expected.len()], expected); |
| |
| Ok(()) |
| } |