Tests/PSModuleBuilder.Integration.Tests.ps1

$CurrentPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$ModulePath = Split-Path -Parent  $CurrentPath
Import-Module $ModulePath\PSModuleBuilder.psm1

Describe "Current Path" {
  Context "When calling from anywhere" {
    It "Should be the module /Tests/ folder"{
      $CurrentPath | Should -Be "C:\side\mine\PSModuleBuilder\PSModuleBuilder\Tests"
    }
  }
}

Describe "Module Path" {
  Context "When calling from anywhere" {
    It "Should be the module ./ folder"{
      $ModulePath | Should -Be "C:\side\mine\PSModuleBuilder\PSModuleBuilder"
    }
  }
}

Describe "Initializing" {
  AfterAll {
    Set-Location $ModulePath
    Remove-Module PSModuleBuilder
  }

  Context "When calling with default params" {

    New-Item TestDrive:\Integration-Test-PsModuleBuilder-Default -ItemType Directory

    Set-Location TestDrive:\Integration-Test-PsModuleBuilder-Default

    Initialize-Psm -Default
    $data = "# New Powershell Module $([Environment]::NewLine) with stuff..."
    Add-PsmData -dataName "readme.md" -data $data
    Add-PsmFunction -functionName "Run-ThePublicPart" -alias "rtpp"
    Add-PsmInternalFunction -functionName "Run-TheInternalPart"
    Write-PsmManifest
    # Publish-PsmModule
    
    ## TODO: v2?
    # Add-PsmDependency -ModuleName "IISAdministration"
    # Start-Psm
    # Start-Psm "Get-ChildItem"
    # Test-Psm
    # Test-Psm "Get-ChildItem"
    # Stop-Psm
    # Stop-Psm "Get-ChildItem"
    $psd1Xml = Import-Clixml .\module-psd1.xml
    $psd1 = Test-ModuleManifest .\compare.psd1

    It "Should create root module with the parent folder name .psm1"{
      $psd1Xml.RootModule | Should -Be "Integration-Test-PsModuleBuilder-Default.psm1"
      $psd1.RootModule | Should -Be "Integration-Test-PsModuleBuilder-Default.psm1"
    }

    It "Should create module name with the parent folder name"{
      $psd1Xml.ModuleName | Should -Be "Integration-Test-PsModuleBuilder-Default"
      $psd1.ModuleName | Should -Be "Integration-Test-PsModuleBuilder-Default"
    }

    @( #folders
      "Data",
      "Public",
      "Internal",
      "Module-Start",
      "Module-Test",
      "Module-Stop"
    ) | ForEach-Object {
      It "Should create module folders: $_"{
        Test-Path .\$_ -PathType Container | Should -Be $true
      }
    }

    @( #files
      "Data\readme.md",
      "Public\Run-ThePublicPart",
      "Internal\Run-TheInternalPart",
      "compare.psd1"
    ) | ForEach-Object {
      It "Should create function/data files: $_"{
        Test-Path .\$_ -PathType Leaf | Should -Be $true
      }
    }

    It "Should create a default command prefix using the first three letters of the parent folder name"{
      $psd1Xml.DefaultCommandPrefix | Should -Be "Int"
      $psd1.DefaultCommandPrefix | Should -Be "Int"
    }

    It "Should create a ModuleVersion of 0.0.0"{
      $psd1Xml.ModuleVersion | Should -Be "0.0.0"
      $psd1.ModuleVersion | Should -Be "0.0.0"
    }

    It "Should create a Author of `$env`:UserName"{
      $psd1Xml.Author | Should -Be "$env:UserName"
      $psd1.Author | Should -Be "$env:UserName"
    }

    It "Should create a CompanyName of `"`""{
      $psd1Xml.CompanyName | Should -Be ""
      $psd1.CompanyName | Should -Be ""
    }
    
    It "Should create a Copyright of `"(c) `$((Get-Date).Year)...`""{
      $psd1Xml.Copyright | Should -Be "(c) $((Get-Date).Year)..."
      $psd1.Copyright | Should -Be "(c) $((Get-Date).Year)..."
    }

    It "Should create a Description of `"`""{
      $psd1Xml.Description | Should -Be ""
      $psd1.Description | Should -Be ""
    }

    It "Should create a Tags of `"`""{
      $psd1Xml.Tags | Should -Be ""
      $psd1.Tags | Should -Be ""
    }

    It "Should create a ProjectUri of `"`""{
      $psd1Xml.ProjectUri | Should -Be ""
      $psd1.ProjectUri | Should -Be ""
    }

    It "Should create a valid & publishable manifest file for the new module" {
      Test-ModuleManifest $ManifestPath -ErrorAction Stop -WarningAction SilentlyContinue
    }

  }

  Context "When collecting params from read-host" {

    New-Item TestDrive:\Integration-Test-PsModuleBuilder -ItemType Directory

    Set-Location TestDrive:\Integration-Test-PsModuleBuilder
    InModuleScope PSModuleBuilder {

      $parentFolder = (Get-Location).path.split("\")[-1]
      $prefix = $parentFolder.Substring(0,3)

      Mock Read-Host { "Corporate-Lyga-Initiative" } -ParameterFilter { $Prompt -eq "RootModule ($parentFolder)" }
      Mock Read-Host { "CLI" } -ParameterFilter { $Prompt -eq "DefaultCommandPrefix ($prefix)" }
      Mock Read-Host { "0.0.1" } -ParameterFilter { $Prompt -eq "ModuleVersion (0.0.0)" }
      Mock Read-Host { "Hubert" } -ParameterFilter { $Prompt -eq "Author ($env:UserName)" }
      Mock Read-Host { "The PsModuleBuilder Company" } -ParameterFilter { $Prompt -eq "CompanyName" }
      Mock Read-Host { "All rights reserved..." } -ParameterFilter { $Prompt -eq "Copyright ((c) $((Get-Date).Year)...)" }
      Mock Read-Host { "Very Good Module!!!" } -ParameterFilter { $Prompt -eq "Description" }
      Mock Read-Host { "Good Stuff" } -ParameterFilter { $Prompt -eq "Tags (space separated)" }
      Mock Read-Host { "https://www.powershellgallery.com" } -ParameterFilter { $Prompt -eq "ProjectUri" }
      
      Initialize-Psm 

      $psd1 = Import-Clixml .\module-psd1.xml

      It "Should create root module with Corporate-Lyga-Initiative.psm1"{
        $psd1.RootModule | Should -Be "Corporate-Lyga-Initiative.psm1"
      }

      It "Should create module name with Corporate-Lyga-Initiative"{
        $psd1.ModuleName | Should -Be "Corporate-Lyga-Initiative"
      }

      @(
        "Data",
        "Public",
        "Internal",
        "Module-Start",
        "Module-Test",
        "Module-Stop"
      ) | ForEach-Object {
        It "Should create module folders: $_"{
          Test-Path .\$_ | Should -Be $true
        }
      }

      It "Should create a default command prefix using CLI"{
        $psd1.DefaultCommandPrefix | Should -Be "CLI"
      }

      It "Should create a ModuleVersion of 0.0.1"{
        $psd1.ModuleVersion | Should -Be "0.0.1"
      }

      It "Should create a Author of 'Hubert'"{
        $psd1.Author | Should -Be "Hubert"
      }

      It "Should create a CompanyName of 'The PsModuleBuilder Company'"{
        $psd1.CompanyName | Should -Be "The PsModuleBuilder Company"
      }
      
      It "Should create a Copyright of 'All rights reserved...'"{
        $psd1.Copyright | Should -Be "All rights reserved..."
      }

      It "Should create a Description of 'Very Good Module!!!'"{
        $psd1.Description | Should -Be "Very Good Module!!!"
      }

      It "Should create a Tags of 'Good', 'Stuff'"{
        $psd1.Tags | Should -Be @("Good", "Stuff")
      }

      It "Should create a ProjectUri of 'https://www.powershellgallery.com'"{
        $psd1.ProjectUri | Should -Be "https://www.powershellgallery.com"
      }
    }
  }
}