fn caching_download_fails_if_hash_incorrect() -> Result<(), CliError> {
        let temp_dir = TempDir::new("example").expect("could not create tempdir");
        let dest_dir = temp_dir.path();

        let file_name = "gs1.zip";
        let temp_file_name = "gs1-temp.zip";

        let file_path = dest_dir.join(file_name);
        let temp_file_path = dest_dir.join(temp_file_name);

        assert!(!file_path.exists());

        let mut downloader = MockDownloader::new(vec![Ok(())]);
        let mut validator =
            MockValidator::new(vec![Err(CliError::ActionError("hash failure".to_string()))]);

        let url = Url::parse("http://localhost/fake").expect("could not create url");

        let config = CachingDownloadConfig {
            url: url.clone(),
            file_path: file_path.to_path_buf(),
            temp_file_path: temp_file_path.to_path_buf(),
            #[cfg(feature = "xsd-downloader-force-download")]
            force_download: false,
            hash: TEST_HASH,
        };

        // Run the test
        let result = caching_download(
            config,
            |url: &Url, file_name: &str| downloader.download(url, file_name),
            |path_buf: &PathBuf, hash: &str| validator.validate(path_buf, hash),
        );

        assert_eq!(
            validator.get_calls(),
            &[MockValidatorCall {
                path_buf: temp_file_path.to_path_buf(),
                hash: TEST_HASH.to_string(),
            }]
        );
        assert!(result.is_err());
        assert_eq!(
            downloader.get_calls(),
            &[MockDownloaderCall {
                url,
                file_name: dest_dir.join(temp_file_name).to_string_lossy().to_string()
            }]
        );
        assert!(!temp_file_path.exists());
        Ok(())
    }
// Test that CachingDownloader fails if the temporary file already exists
Test that CachingDownloader fails if the temporary file already exists
Test that CachingDownloader fails if the temporary file already exists
fn caching_download_fails_if_hash_incorrect() -> Result<(), CliError> {
        let temp_dir = TempDir::new("example").expect("could not create tempdir");
        let dest_dir = temp_dir.path();
        let file_name = "gs1.zip";
        let temp_file_name = "gs1-temp.zip";
        let file_path = dest_dir.join(file_name);
        let temp_file_path = dest_dir.join(temp_file_name);
        assert!(!file_path.exists());
        let mut downloader = MockDownloader::new(vec![Ok(())]);
        let mut validator =
            MockValidator::new(vec![Err(CliError::ActionError("hash failure".to_string()))]);
        let url = Url::parse("http://localhost/fake").expect("could not create url");
        let config = CachingDownloadConfig {
            url: url.clone(),
            file_path: file_path.to_path_buf(),
            temp_file_path: temp_file_path.to_path_buf(),
            #[cfg(feature = "xsd-downloader-force-download")]
            force_download: false,
            hash: TEST_HASH,
        };
        // Run the test
        let result = caching_download(
            config,
            |url: &Url, file_name: &str| downloader.download(url, file_name),
            |path_buf: &PathBuf, hash: &str| validator.validate(path_buf, hash),
        );
        assert_eq!(
            validator.get_calls(),
            &[MockValidatorCall {
                path_buf: temp_file_path.to_path_buf(),
                hash: TEST_HASH.to_string(),
            }]
        );
        assert!(result.is_err());
        assert_eq!(
            downloader.get_calls(),
            &[MockDownloaderCall {
                url,
                file_name: dest_dir.join(temp_file_name).to_string_lossy().to_string()
            }]
        );
        assert!(!temp_file_path.exists());
        Ok(())
    }
fn caching_download_fails_if_hash_incorrect ( ) -> Result < ( ) , CliError > { let temp_dir = TempDir :: new ( " " ) . expect ( " " ) ; let dest_dir = temp_dir . path ( ) ; let file_name = " " ; let temp_file_name = " " ; let file_path = dest_dir . join ( file_name ) ; let temp_file_path = dest_dir . join ( temp_file_name ) ; assert ! ( file_path exists ( ) ) ; let mut downloader = MockDownloader :: new ( vec ! [ Ok ( ( ) ) ] ) ; let mut validator = MockValidator :: new ( vec ! [ Err ( CliError ActionError ( " " to_string ( ) ) ) ] ) ; let url = Url :: parse ( " " ) . expect ( " " ) ; let config = CachingDownloadConfig { url : url . clone ( ) , file_path : file_path . to_path_buf ( ) , temp_file_path : temp_file_path . to_path_buf ( ) , # [ cfg ( feature " " ) ] force_download : false , hash : TEST_HASH , } ; // Run the test let result = caching_download ( config , | url : & Url , file_name : & str | downloader . download ( url , file_name ) , | path_buf : & PathBuf , hash : & str | validator . validate ( path_buf , hash ) , ) ; assert_eq ! ( validator get_calls ( ) [ MockValidatorCall { path_buf temp_file_path to_path_buf ( ) hash TEST_HASH to_string ( ) } ] ) ; assert ! ( result is_err ( ) ) ; assert_eq ! ( downloader get_calls ( ) [ MockDownloaderCall { url file_name dest_dir join ( temp_file_name ) to_string_lossy ( ) to_string ( ) } ] ) ; assert ! ( temp_file_path exists ( ) ) ; Ok ( ( ) ) }
Test that CachingDownloader fails if the temporary file already exists
Test that CachingDownloader fails if the temporary file already exists