Public/New-PsModule.tests.ps1

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
. "$here\Get-PsModuleBuilderPath.ps1"
. "$here\Set-PsModuleBuilderPath.ps1"

function Get-ExpectedFilesAndDirs {
  param( 
    [string]$path,
    [string]$moduleName
  )

  return @{
    files = @(
      "$path\$moduleName\Data\plasterManifest.xml",
      "$path\$moduleName\$moduleName.psd1",
      "$path\$moduleName\$moduleName.psm1",
      "$path\$moduleName\readme.md"
    )
    directories = @(
      "$path\$moduleName",
      "$path\$moduleName\Data",
      "$path\$moduleName\Internal",
      "$path\$moduleName\Public",
      "$path\$moduleName\Tests"
    )
  }
}

function Restore-TheTest {
  param([string]$path)

  Write-Host "Formatting the test with: $path"

  Remove-Item "TestDrive:\*" -Recurse -Force
  New-Item $path -ItemType Directory -Force
  Set-PsModuleBuilderPath $path
}

Describe New-PsModule {
  Context "When only name is provided..." {

    $testPath = "TestDrive:\Modules"
    $testModule = "TestModule"

    Restore-TheTest $testPath

    New-PsModule $testModule

    $expected = Get-ExpectedFilesAndDirs $testPath $testModule
    
    $expected.files | ForEach-Object {
      It "Should create file $_" {
        Test-Path $_ -PathType Leaf | Should -Be $true
      }
    }

    $expected.directories | ForEach-Object {
      It "Should create directory $_" {
        Test-Path $_ -PathType Container | Should -Be $true
      }
    }
  }

  Context "When name and path are provided..." {
    $testModule = "TestModule"
    $testPath = "TestDrive:\Other-Modules"

    Restore-TheTest $testPath

    New-PsModule $testModule $testPath

    $expected = Get-ExpectedFilesAndDirs $testPath $testModule

    $expected.directories | ForEach-Object {
      It "Should create directory $_" {
        Test-Path $_ -PathType Container | Should -Be $true
      }
    }

    $expected.files | ForEach-Object {
      It "Should create file $_" {
        Test-Path $_ -PathType Leaf | Should -Be $true
      }
    }
  }
}