// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::sync::Arc;

use anyhow::Result;
use tracing::instrument;

use crate::Framework;

/// A wrapper containing the objects needed to run the executor's test fixtures.
pub struct TestFixtures {
    test_context: Arc<Framework>,
}

impl TestFixtures {
    /// Creates a new set of test fixtures using the supplied command-line
    /// parameters and artifact store.
    pub fn new(test_context: Arc<Framework>) -> Result<Self> {
        Ok(Self { test_context })
    }

    /// Calls fixture routines that need to run before any tests run.
    #[instrument(skip_all)]
    pub fn execution_setup(&mut self) -> Result<()> {
        Ok(())
    }

    /// Calls fixture routines that need to run after all tests run.
    ///
    /// Unless the runner panics, or a test panics in a way that can't be caught
    /// during unwinding, this cleanup fixture will run even if a test run is
    /// interrupted.
    #[instrument(skip_all)]
    pub fn execution_cleanup(&mut self) -> Result<()> {
        Ok(())
    }

    /// Calls fixture routines that run before each test case is invoked.
    #[instrument(skip_all)]
    pub fn test_setup(&mut self) -> Result<()> {
        Ok(())
    }

    /// Calls fixture routines that run after each test case is invoked.
    ///
    /// Unless the runner panics, or a test panics in a way that can't be caught
    /// during unwinding, this cleanup fixture will run whenever the
    /// corresponding setup fixture has run.
    #[instrument(skip_all)]
    pub async fn test_cleanup(&mut self) -> Result<()> {
        self.test_context.reset().await;
        Ok(())
    }
}
