gaelcolas / Sampler

Module template with build pipeline and examples, including DSC elements.
MIT License
167 stars 42 forks source link

QA module tests doesn't work correctly when module located in the git repo subfolder #465

Closed Sidoran closed 5 months ago

Sidoran commented 6 months ago

Problem description

The module.tests.ps1 doesn't correctly identify that the module root folder is different from the git top-level folder. This problem is specific to the repos with the multiple modules in it.

Verbose logs

[-] Changelog has been updated 130ms (124ms|6ms)
   Expected 'CHANGELOG.md' to be found in collection NewModule/CHANGELOG.md, because the CHANGELOG.md must be updated with at least one entry in the Unreleased section for each PR, but it was not found.
   at $filesChanged | Should -Contain 'CHANGELOG.md' -Because 'the CHANGELOG.md must be updated with at least one entry in the Unreleased section for each PR', C:\Repos\TestSampler\NewModule\tests\QA\module.tests.ps1:82
   at <ScriptBlock>, C:\Repos\TestSampler\NewModule\tests\QA\module.tests.ps1:82

How to reproduce

  1. Create a new folder and execute git init in it. I used C:\Repos\TestSampler
  2. Execute New-SampleModule and put the folder path from Step 1 to a DestinationPath. New-SampleModule -ModuleName NewModule -DestinationPath C:\Repos\TestSampler\ -ModuleType SimpleModule -ModuleAuthor "Authorio" -ModuleDescription "Just test sampler module"
  3. Commit all changes to a main branch.
  4. Create another branch.
  5. Change the CHANGELOG.md file and save the changes.
  6. Execute .\build.ps1 -Tasks build
  7. Execute .\build.ps1 -Tasks test
  8. See error.

Expected behavior

The module.tests.ps1 should match the git top level with the ProjectPath and correctly derive the ChangeLog.md file path.

Current behavior

The module.tests.ps1 is looking for ChangeLog.md in the git top-level folder.

Suggested solution

Add $gitRelatedModulePath to a BeforeAll section

$gitTopLevelPath = (&git rev-parse --show-toplevel)
$gitRelatedModulePath = (($projectPath -replace [regex]::Escape([IO.Path]::DirectorySeparatorChar), '/') -replace $gitTopLevelPath, '').Trim('/') + '/'

Change the $filesChanged and $filesStagedAndUnstaged in the It 'Changelog has been updated'

$filesChanged = (&git @('diff', "$defaultBranchCommit...$headCommit", '--name-only')) -replace "^$gitRelatedModulePath", ""
$filesStagedAndUnstaged = (&git @('diff', 'HEAD', '--name-only') 2>&1) -replace "^$gitRelatedModulePath", ""

Operating system the target node is running

OsName               : Microsoft Windows 11 Pro
OsOperatingSystemSKU : 48
OsArchitecture       : 64-bit
WindowsVersion       : 2009
WindowsBuildLabEx    : 22621.1.amd64fre.ni_release.220506-1250
OsLanguage           : en-US
OsMuiLanguages       : {en-US, uk-UA}
Also tested on Win 10 Pro

PowerShell version and build the target node is running

PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module version used

Sampler 0.117.0 C:\Repos\TestSampler\NewModule\output\RequiredModules\Sampler\0.117.0\Sampler.psd1
johlju commented 6 months ago

Maybe we can use Get-SamplerModuleRootPath instead to get the module root path? If other tasks work then that might solve it too?

Sidoran commented 6 months ago

Maybe we can use Get-SamplerModuleRootPath instead to get the module root path? If other tasks work then that might solve it too?

Sorry, @johlju , I didn't get how the Get-SamplerModuleRootPath may help solve the issue. As I understand, the Get-SamplerModuleRootPath outputs the *.psm1 file path, but the problem in the module.tests.ps1 that receives from git diff list of the files with the Git Top level related path as you may see on a screenshot: image So the module.tests.ps1.template#L86 rule will fail.

johlju commented 6 months ago

Ah sorry. I misunderstood the issue. I'm following you now!