-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor test code so it can be excluded from code coverage (#66)
Test code is not part of the library as consumed by third parties, so (IMHO) should not be included in coverage reports. Since it's not (yet?) possible to exclude code at function or block level in stable builds, using this approach so that test code can be excluded via [cargo-llvm-cov's exclude by file path option](https://github.com/taiki-e/cargo-llvm-cov#exclude-file-from-coverage).
- Loading branch information
1 parent
aef54aa
commit d3fe926
Showing
11 changed files
with
228 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2022 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
use std::{ | ||
env, fs, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
pub(crate) fn fixture_path(name: &str) -> String { | ||
let root_dir = &env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
|
||
let mut path = PathBuf::from(root_dir); | ||
path.push("src/tests/fixtures"); | ||
path.push(name); | ||
|
||
assert!(path.exists()); | ||
|
||
path.to_str().unwrap().to_string() | ||
} | ||
|
||
pub(crate) fn temp_copy_of_fixture(tempdir: &Path, name: &str) -> String { | ||
let fixture_src = fixture_path(name); | ||
let fixture_path = Path::join(tempdir, name); | ||
let fixture_copy = fixture_path.as_path(); | ||
|
||
fs::copy(fixture_src, fixture_copy).unwrap(); | ||
fixture_copy.display().to_string() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2022 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
// Tests are grouped under this module so as to avoid | ||
// having the test code itself included in coverage numbers. | ||
|
||
mod fixtures; | ||
mod xmp_date_time; | ||
mod xmp_file; | ||
mod xmp_meta; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2020 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
use crate::XmpDateTime; | ||
|
||
#[test] | ||
fn new_empty() { | ||
let mut _dt = XmpDateTime::new(); | ||
} | ||
|
||
#[test] | ||
fn current() { | ||
let mut _dt = XmpDateTime::current(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2020 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
use std::path::PathBuf; | ||
|
||
use tempfile::tempdir; | ||
|
||
use crate::{tests::fixtures::*, xmp_ns, OpenFileOptions, XmpDateTime, XmpFile, XmpMeta}; | ||
|
||
#[test] | ||
fn open_and_edit_file() { | ||
let tempdir = tempdir().unwrap(); | ||
let purple_square = temp_copy_of_fixture(tempdir.path(), "Purple Square.psd"); | ||
|
||
{ | ||
let mut f = XmpFile::new(); | ||
|
||
assert!(f | ||
.open_file( | ||
&purple_square, | ||
OpenFileOptions::default().for_update().use_smart_handler() | ||
) | ||
.is_ok()); | ||
|
||
let opt_m = f.xmp(); | ||
assert!(opt_m.is_some()); | ||
|
||
XmpMeta::register_namespace("http://purl.org/dc/terms/", "dcterms"); | ||
|
||
let mut m = opt_m.unwrap(); | ||
m.set_property("http://purl.org/dc/terms/", "provenance", "blah"); | ||
|
||
assert!(m.does_property_exist("http://purl.org/dc/terms/", "provenance")); | ||
assert!(!m.does_property_exist("http://purl.org/dc/terms/", "provenancx")); | ||
|
||
if m.does_property_exist(xmp_ns::XMP, "MetadataDate") { | ||
let updated_time = XmpDateTime::current(); | ||
m.set_property_date(xmp_ns::XMP, "MetadataDate", &updated_time); | ||
} | ||
|
||
assert!(f.can_put_xmp(&m)); | ||
f.put_xmp(&m); | ||
|
||
f.close(); | ||
} | ||
|
||
// Let's make sure we actually wrote to the file. | ||
{ | ||
let mut f = XmpFile::new(); | ||
|
||
assert!(f | ||
.open_file( | ||
&purple_square, | ||
OpenFileOptions::default().for_update().use_smart_handler() | ||
) | ||
.is_ok()); | ||
|
||
let m = f.xmp().unwrap(); | ||
|
||
assert_eq!( | ||
m.property("http://purl.org/dc/terms/", "provenance") | ||
.unwrap(), | ||
"blah" | ||
); | ||
assert_eq!(m.property("http://purl.org/dc/terms/", "provenancx"), None); | ||
} | ||
} | ||
|
||
#[test] | ||
fn open_fail() { | ||
let bad_path = PathBuf::from("doesnotexist.jpg"); | ||
|
||
{ | ||
let mut f = XmpFile::new(); | ||
|
||
assert!(f | ||
.open_file( | ||
&bad_path, | ||
OpenFileOptions::default().for_update().use_smart_handler() | ||
) | ||
.is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2020 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
use crate::{tests::fixtures::*, XmpMeta}; | ||
|
||
#[test] | ||
fn new_empty() { | ||
let mut _m = XmpMeta::new(); | ||
} | ||
|
||
#[test] | ||
fn from_file() { | ||
let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap(); | ||
|
||
assert_eq!( | ||
m.property("http://ns.adobe.com/xap/1.0/", "CreatorTool") | ||
.unwrap(), | ||
"Adobe Photoshop CS2 Windows" | ||
); | ||
|
||
assert_eq!( | ||
m.property("http://ns.adobe.com/photoshop/1.0/", "ICCProfile") | ||
.unwrap(), | ||
"Dell 1905FP Color Profile" | ||
); | ||
|
||
assert!(m | ||
.property("http://ns.adobe.com/photoshop/1.0/", "ICCProfilx") | ||
.is_none()); | ||
} | ||
|
||
#[test] | ||
fn register_namespace() { | ||
assert_eq!( | ||
XmpMeta::register_namespace("http://purl.org/dc/terms/", "dcterms"), | ||
"dcterms:" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.