11"""Function to test the CLI"""
2-
2+ import os
33import subprocess
44
5+ import rasterio
6+
57import xdem
8+ from xdem import dem_coregistration
69
710
811class TestCLI :
912 # Define paths to the DEM files using xDEM examples
1013 ref_dem_path = xdem .examples .get_path ("longyearbyen_ref_dem" )
1114 tba_dem_path = xdem .examples .get_path ("longyearbyen_tba_dem" )
15+ aligned_dem_path = "aligned_dem.tiff"
16+ inlier_mask_path = "inlier_mask.tiff"
1217
1318 def test_xdem_cli (self ) -> None :
1419 try :
@@ -18,9 +23,32 @@ def test_xdem_cli(self) -> None:
1823 capture_output = True ,
1924 text = True ,
2025 )
21- assert "hello world" in result .stdout
26+
27+ # Assert ClI ran successfully
2228 assert result .returncode == 0
2329
30+ # Verify the existence of the output files
31+ assert os .path .exists (self .aligned_dem_path ), f"Aligned DEM not found: { self .aligned_dem_path } "
32+ assert os .path .exists (self .inlier_mask_path ), f"Inlier mask not found: { self .inlier_mask_path } "
33+
34+ # Retrieve ground truth
35+ true_coreg_dem , coreg_method , out_stats , true_inlier_mask = dem_coregistration (
36+ xdem .DEM (self .tba_dem_path ), xdem .DEM (self .ref_dem_path ), self .aligned_dem_path
37+ )
38+
39+ # Load elements processed by the xDEM CLI command
40+ aligned_dem = xdem .DEM (self .aligned_dem_path )
41+ with rasterio .open (self .inlier_mask_path ) as src :
42+ inlier_mask = src .read (1 )
43+
44+ # Verify match with ground truth
45+ assert aligned_dem == true_coreg_dem , "Aligned DEM does not match the ground truth."
46+ assert inlier_mask .all () == true_inlier_mask .all (), "Inlier mask does not match the ground truth."
47+
48+ # Erase files
49+ os .remove (self .aligned_dem_path )
50+ os .remove (self .inlier_mask_path )
51+
2452 except FileNotFoundError as e :
2553 # In case 'xdem' is not found
2654 raise AssertionError (f"CLI command 'xdem' not found : { e } " )
0 commit comments