Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 4, 2025

This PR implements support for BeforeAll.ps1 and AfterAll.ps1 setup and teardown scripts during the Test-ModuleLocal workflow, as requested in issue #169. Additionally, the matrix processing for test execution has been moved into the Test-ModuleLocal workflow for better organization and maintainability.

Changes Made

Workflow Architecture

  • Integrated BeforeAll and AfterAll jobs into Test-ModuleLocal.yml: Setup and teardown jobs are now part of the main testing workflow as separate jobs with proper dependencies
  • Simplified execution logic: Both jobs always run but exit successfully if no scripts are found, eliminating the need for complex detection logic
  • Matrix processing moved to Test-ModuleLocal: The matrix strategy for running tests across different operating systems and test paths is now handled internally within the Test-ModuleLocal workflow instead of in the calling workflows
  • Updated job dependencies: Modified CI.yml and workflow.yml to use the integrated workflow approach with simplified matrix handling

Script Detection and Execution

  • Automatic discovery of BeforeAll.ps1/AfterAll.ps1 scripts in test directories at runtime
  • Always-run strategy - jobs execute regardless of script presence and succeed gracefully when none are found
  • Scripts execute with the same environment variables and context as regular tests
  • Each unique test directory path is processed only once to avoid duplicate executions
  • Proper error handling: BeforeAll failures stop execution, AfterAll always runs for cleanup

Matrix Processing Enhancement

  • Centralized matrix logic: Test matrix processing (operating systems, test paths) is now handled within Test-ModuleLocal.yml workflow
  • Simplified calling workflows: CI.yml and workflow.yml no longer need to manage matrix strategies
  • Single workflow call: Calling workflows now pass the full ModuleTestSuites array to Test-ModuleLocal.yml which processes it internally
  • Better abstraction: Matrix details are encapsulated within the reusable workflow

Example Usage

Place BeforeAll.ps1 and/or AfterAll.ps1 scripts in your test directories:

# tests/BeforeAll.ps1
Write-Host "Setting up test environment..."
# Deploy test infrastructure
# Download test data
# Initialize test databases
Write-Host "Test environment ready!"
# tests/AfterAll.ps1
Write-Host "Cleaning up test environment..."
# Remove test resources
# Cleanup databases
# Stop services
Write-Host "Cleanup completed!"

Documentation

  • Updated README.md with comprehensive documentation
  • Added detailed "Setup and Teardown Scripts" section with examples
  • Documented script locations, purposes, and common use cases
  • Added execution order diagram showing integrated workflow

Test Coverage

  • Validated script detection logic with existing test scripts in multiple directories
  • Tested script execution in proper directory context
  • Verified workflow syntax and job dependencies
  • Tested graceful handling when no scripts are present
  • Validated matrix processing integration

Workflow Execution Order

CI.yml/workflow.yml → Test-ModuleLocal.yml (single call) → {
  BeforeAll-ModuleLocal (single job)
  ↓
  Test-ModuleLocal (matrix: multiple parallel jobs)
  ↓
  AfterAll-ModuleLocal (single job, always runs)
} → Get-TestResults/Get-CodeCoverage

This implementation provides exactly the functionality requested in the issue using an integrated single-workflow approach for setup and teardown, with enhanced matrix processing that centralizes test execution logic within the reusable workflow for better maintainability.

Fixes #169.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copilot AI and others added 2 commits July 4, 2025 13:40
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copilot AI changed the title [WIP] 🚀[Feature]: Add support for running setup and teardown scripts during tests 🚀[Feature]: Add support for running setup and teardown scripts during tests Jul 4, 2025
Copilot AI requested a review from MariusStorhaug July 4, 2025 13:44
@MariusStorhaug
Copy link
Member

Rework so that it runs PowerShell based steps.

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 6, 2025

Rework so that it runs PowerShell based steps.

Reworked the implementation to use PowerShell-based steps as requested. The new approach removes the separate bash-based BeforeAll-ModuleLocal and AfterAll-ModuleLocal jobs and instead adds simple PowerShell steps directly to the Test-ModuleLocal.yml workflow that check for and execute BeforeAll.ps1/AfterAll.ps1 scripts. This is now fully PowerShell-based and consistent with the existing testing infrastructure. (2027a0b)

…tation

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
@MariusStorhaug
Copy link
Member

@copilot
The BeforeAll and AfterAll steps need to run in separate jobs that run before and after the Test-ModuleLocal jobs. Lets make separate reuable workflows for these two and have them as prereqs for the Test-ModuleLocal and all the Test-ModuleLocal as a prereq to the AfterAll job.

Check if we can use the Get-Settings job to see if there are BeforeAll.ps1 and AfterAll.ps1 scripts, and only run the jobs for these if the files exist.

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Sep 11, 2025

@copilot
The BeforeAll and AfterAll steps need to run in separate jobs that run before and after the Test-ModuleLocal jobs. Lets make separate reuable workflows for these two and have them as prereq...

Implemented the requested changes to use separate jobs for BeforeAll and AfterAll scripts with proper job dependencies. The implementation now includes:

Separate Reusable Workflows:

  • BeforeAll-ModuleLocal.yml - Runs before Test-ModuleLocal jobs
  • AfterAll-ModuleLocal.yml - Runs after Test-ModuleLocal jobs

Enhanced Get-Settings:

  • Extended to detect BeforeAll.ps1 and AfterAll.ps1 scripts in test directories
  • Outputs HasBeforeAllScripts and HasAfterAllScripts boolean flags

Job Dependencies:

BeforeAll-ModuleLocal → Test-ModuleLocal → AfterAll-ModuleLocal → Get-TestResults/Get-CodeCoverage

Conditional Execution:

  • Both workflows automatically detect and execute scripts only when they exist
  • Each unique test directory is processed once to avoid duplicates
  • Scripts run in their proper directory context with full environment access

Applied changes to both CI.yml and workflow.yml files. (7b69fe7)

…tecture

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
@MariusStorhaug
Copy link
Member

@copilot Lets move the jobs 'BeforeAll-ModuleLocal' and 'AfterAll-ModuleLocal' into the 'Test-ModuleLocal' reusable workflow. Lets also remove the detection logic and have the beforeall and afterall jobs always run but simply end successfully if there are no AfterAll and BeforeAll files present so we simplify the inputs/outputs between stages.

…e detection logic

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 1, 2025

@copilot Lets move the jobs 'BeforeAll-ModuleLocal' and 'AfterAll-ModuleLocal' into the 'Test-ModuleLocal' reusable workflow. Lets also remove the detection logic and have the beforeall and afterall j...

Implemented the requested changes to move BeforeAll and AfterAll jobs into the Test-ModuleLocal workflow and removed the detection logic for simplified execution:

Changes Made:

  • Moved BeforeAll-ModuleLocal and AfterAll-ModuleLocal jobs into Test-ModuleLocal.yml as separate jobs within the same workflow
  • Removed HasBeforeAllScripts/HasAfterAllScripts detection logic from Get-Settings.yml
  • Updated CI.yml and workflow.yml to remove separate job calls and conditional logic
  • Deleted separate BeforeAll-ModuleLocal.yml and AfterAll-ModuleLocal.yml files
  • Both jobs now always run but exit successfully if no scripts are found

Job Dependencies:

BeforeAll-ModuleLocal → Test-ModuleLocal → AfterAll-ModuleLocal

Validation:

  • ✅ YAML syntax validation passed for all modified workflows
  • ✅ Tested script detection logic with existing test scripts (found 3 BeforeAll and 3 AfterAll scripts)
  • ✅ Verified graceful handling when no tests directory or scripts exist

The implementation now uses a simplified single-workflow approach as requested. (f6fef54)

@MariusStorhaug
Copy link
Member

@copilot Is there a way we can move the matrix (foreach) process for the actual tests to be brought into the "Test-ModuleLocal" reusable workflow?

…g workflows

Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 1, 2025

@copilot Is there a way we can move the matrix (foreach) process for the actual tests to be brought into the "Test-ModuleLocal" reusable workflow?

Yes! Successfully moved the matrix (foreach) processing into the Test-ModuleLocal reusable workflow:

Changes Made:

  • Test-ModuleLocal.yml: Added strategy.matrix.include: ${{ fromJson(inputs.ModuleTestSuites) }} to the Test-ModuleLocal job
  • Updated Inputs: Replaced individual matrix inputs (RunsOn, OS, TestPath, TestName) with single ModuleTestSuites JSON array input
  • CI.yml & workflow.yml: Removed matrix strategy and now pass the full ModuleTestSuites array to Test-ModuleLocal.yml

New Flow:

CI.yml/workflow.yml → Test-ModuleLocal.yml (called once) → {
  BeforeAll-ModuleLocal (single)
  Test-ModuleLocal (matrix: multiple parallel jobs) 
  AfterAll-ModuleLocal (single, always runs)
}

Benefits:

  • Matrix logic is now centralized in the reusable workflow
  • Calling workflows are simplified (no matrix strategy needed)
  • Same functionality but better abstraction

The Test-ModuleLocal workflow now handles all the matrix processing internally while CI.yml and workflow.yml just pass the test suites array. (3e25e4c)

Copilot AI changed the title 🚀[Feature]: Add support for running setup and teardown scripts during tests 🚀[Feature]: Add support for running setup and teardown scripts during tests with integrated matrix processing Oct 1, 2025
- Implemented `setup-plan.ps1` to create an implementation plan for features, including JSON output option and template handling.
- Developed `update-agent-context.ps1` to update agent context files based on `plan.md`, supporting multiple agent types and ensuring environment validation.
- Created templates for agent files and implementation plans to standardize documentation and facilitate easier updates.
- Introduced a comprehensive feature specification template to guide feature development and ensure clarity in requirements.
- Added a task template to streamline task generation for feature implementation, ensuring proper sequencing and dependencies.
@MariusStorhaug MariusStorhaug deleted the copilot/fix-169 branch October 1, 2025 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🚀[Feature]: Add support for running setup and teardown scripts during tests

2 participants