8000 Allow `Test-ModuleManifest` to work when filename is not specified #8388 by pougetat · Pull Request #8687 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Allow Test-ModuleManifest to work when filename is not specified #8388#8687

Merged
daxian-dbw merged 1 commit intoPowerShell:masterfrom
pougetat:fix-testmodulemanifest-command
Mar 10, 2019
Merged

Allow Test-ModuleManifest to work when filename is not specified #8388#8687
daxian-dbw merged 1 commit intoPowerShell:masterfrom
pougetat:fix-testmodulemanifest-command

Conversation

@pougetat
Copy link
@pougetat pougetat commented Jan 18, 2019

PR Summary

Fixing 'Test-Module requires rootmodule entry to be specified with filename extension'
fixes #8388

PR Context

PR Checklist

@pougetat pougetat force-pushed the fix-testmodulemanifest-command branch from 5994fd0 to 33adf5e Compare January 18, 2019 16:14
@daxian-dbw daxian-dbw requested a review from rjmholt January 18, 2019 19:31
@daxian-dbw
Copy link
Member

@rjmholt can you please review this PR?

@daxian-dbw daxian-dbw added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jan 18, 2019
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a good opportunity to label the true parameter in IsValidFilePath

Copy link
Collaborator
@rjmholt rjmholt Jan 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this entire condition really needs to be spun into a bool method. I think it's now too long to be inline here, since it's very hard to read and determine the logic or intention of.

Something like:

...
if (!IsRootModuleValid(module.RootModule))
{
    string errorMsg = StringUtil.Format(Modules.InvalidModuleManifest, module.RootModule, filePath);
    ...
}
...

// All module extensions except ".psd1" are valid RootModule extensions
private static readonly IReadOnlyList<string> s_validRootModuleExtensions = ModuleIntrinsics.PSModuleExtensions
    .Filter(ext => !string.Equals(ext, StringLiterals.PowerShellDataFileExtension, StringComparer.OrdinalIgnoreCase))
    .ToArray();

/// <summary>
/// Checks whether the RootModule field of a module is valid or not.
/// Valid root modules are:
///  - null
///  - Empty string
///  - A valid non-psd1 module file (psm1, cdxml, xaml, dll), as name with extension, name without extension, or path
/// </summary>
/// <param name="rootModule">The root module specified in the module manifest RootModule field.</param>
/// <returns>True if the root module is valid, false otherwise.</returns>
private static bool HasValidRootModule(PSModuleInfo module)
{
    // Empty/null root modules are allowed
    if (string.IsNullOrEmpty(module.RootModule))
    {
        return true;
    }

    // GAC assemblies are allowed
    if (IsValidGacAssembly(module.RootModule))
    {
        return true;
    }

    // Check for extensions
    string rootModuleExt = Path.GetExtension(module.RootModule);
    if (!string.IsNullOrEmpty(rootModuleExt))
    {
        // Check that the root module's extension is an allowed one
        if(!s_validRootModuleExtensions.Contains(rootModuleExt, StringComparison.OrdinalIgnoreCase))
        {
            return false;
        }

        // Check the file path of the full root module
        return IsValidFilePath(module.RootModule, module, verifyPathScope: true);
    }

    // We have no extension, so need to check all of them
    foreach (string extension in s_validRootModuleExtensions)
    {
        if (IsValidFilePath(module.RootModule + extension, module, verifyPathScope: true))
        {
            return true;
        }
    }
    return false;
}

I haven't executed or otherwise tested this code, so it might not even compile, but I think it's worth breaking the logic up in this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice. From what I understand a root module without an extension is considered valid only if the file path with extension is valid and that means we can't just return true if the root module has no file extension as this code suggest.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes sense, good catch.

I've edited the code above. Tell me if that makes more sense.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I'll rewrite the PR on monday using this. Thanks for the help. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prevailing comment style tends to be //-prefixed blocks, rather than /* */ regions:

// RootModule can be null, empty string or point to a valid .psm1, .cdxml, .xaml or .dll. Extensions are optional.
// Anything else is invalid.

If you make the change below, you probably don't need a comment here at all though.

@pougetat pougetat force-pushed the fix-testmodulemanifest-command branch 4 times, most recently from fe1d002 to a57506b Compare January 21, 2019 11:45
@pougetat
Copy link
Author

@rjmholt Hey :), I have updated the PR using your suggestions.

Copy link
Collaborator
@rjmholt rjmholt Jan 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static IList<string> validRootModuleExtensions = ModuleIntrinsics.PSModuleExtensions
private static readonly IReadOnlyList<string> s_validRootModuleExtensions = ModuleIntrinsics.PSModuleExtensions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I can't do s_ due to CodeFactor not allowing it but I can do _ instead which also corresponds to how other parts of the repo do it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's our misconfiguration of CodeFactor. We've discussed it before and agreed that s_ is what we want. The s_ convention is documented here and there's a PR open to update CodeFactor.

Copy link
Author
@pougetat pougetat Jan 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rjmholt ok sounds good. I have just changed it to s_. :)

Copy link
Collaborator
@rjmholt rjmholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pougetat This looks great! If you can make that last change, I can approve

@pougetat pougetat force-pushed the fix-testmodulemanifest-command branch 2 times, most recently from 6dce2ea to 47987be Compare January 22, 2019 22:47
Copy link
Collaborator
@rjmholt rjmholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks again @pougetat.

(I've restarted the tests -- they seem to be caused by unrelated race-conditional failures)

@pougetat
Copy link
Author

Hey @daxian-dbw ,
Is this good to merge ? :)

@TravisEz13 TravisEz13 changed the title Fixing 'Test-Module requires rootmodule entry to be specified with filename extension' #8388 Allow Test-ModuleManifest to work when filename is not specified Jan 26, 2019
@pougetat
Copy link
Author

Hey @rjmholt,
Is it ok to merge this ? :)

@pougetat pougetat changed the title Allow Test-ModuleManifest to work when filename is not specified Allow Test-ModuleManifest to work when filename is not specified #8388 Feb 1, 2019< 8000 /a>
@pougetat pougetat force-pushed the fix-testmodulemanifest-command branch from 47987be to 7e2a84f Compare February 8, 2019 09:10
@stale
Copy link
stale bot commented Mar 10, 2019

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

@stale stale bot added the Stale label Mar 10, 2019
@daxian-dbw daxian-dbw self-assigned this Mar 10, 2019
@stale stale bot removed the Stale label Mar 10, 2019
@daxian-dbw
Copy link
Member

@pougetat I apologize for us being neglecting this PR. I will quickly go through the changes and merge it if things look good.

@daxian-dbw daxian-dbw merged commit e3829cb into PowerShell:master Mar 10, 2019
@pougetat
Copy link
Author

@daxian-dbw No worries. Thanks for merging. :)

@pougetat pougetat deleted the fix-testmodulemanifest-command branch March 11, 2019 09:51
@daxian-dbw
Copy link
Member

@pougetat Thank you for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-ModuleManifest erroneously requires a script-module RootModule entry to be specified with filename extension

4 participants

0