Skip to content

Commit

Permalink
Merge pull request #1287 from sara-rn/linter-nuspec-categories
Browse files Browse the repository at this point in the history
Make linter compatible with new nuspec packages
  • Loading branch information
Ana06 authored Feb 28, 2025
2 parents a740521 + 11070ff commit e161dfc
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions scripts/test/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,40 @@ def check(self, path):
folder = path.parts[-2]

return not (pkg_id == folder == nuspec[:-len(".nuspec")])



class UsesInvalidCategory(Lint):
# The common.vm, debloat.vm, and installer.vm packages are special as they
# assist with the installation and allow to share code between packages.
# They do not install any tool and consequently don't have a category.
EXCLUSIONS = [
"common.vm",
"debloat.vm",
"installer.vm",
# exclude dcode as it fails to install: https://github.com/mandiant/VM-Packages/issues/1176
"dcode.vm",
]
root_path = os.path.abspath(os.path.join(__file__, "../../.."))
categories_txt = os.path.join(root_path, "categories.txt")
with open(categories_txt) as file:
CATEGORIES = [line.rstrip() for line in file]
logger.debug(CATEGORIES)

name = "Uses an invalid category"
recommendation = f"Place a category between <tags> and </tags> from {categories_txt} or exclude the package in the linter"

def check(self, path):
if any([exclusion in str(path) for exclusion in self.EXCLUSIONS]):
return False

# utf-8-sig ignores BOM
file_content = open(path, "r", encoding="utf-8-sig").read()

match = re.search(r"<tags>(?P<category>[\w ]+)<\/tags>", file_content)
if not match or match.group("category") not in self.CATEGORIES:
return True
return False

NUSPEC_LINTS = (
IncludesRequiredFieldsOnly(),
Expand All @@ -281,6 +315,7 @@ def check(self, path):
DependencyContainsUppercaseChar(),
VersionNotUpdated(),
PackageIdNotMatchingFolderOrNuspecName(),
UsesInvalidCategory(),
)


Expand Down Expand Up @@ -321,7 +356,7 @@ def check(self, path):
return not self.FIRST_LINE == lines[0]


class UsesInvalidCategory(Lint):
class UsesCategoryFromNuspec(Lint):
# Some packages don't have a category (we don't create a link in the tools directory)
EXCLUSIONS = [
".dbgchild.vm",
Expand All @@ -343,40 +378,36 @@ class UsesInvalidCategory(Lint):
"x64dbgpy.vm",
"vscode.extension.",
"chrome.extensions.vm",
# exclude dcode as it fails to install: https://github.com/mandiant/VM-Packages/issues/1176
"dcode.vm",
]

root_path = os.path.abspath(os.path.join(__file__, "../../.."))
categories_txt = os.path.join(root_path, "categories.txt")
with open(categories_txt) as file:
CATEGORIES = [line.rstrip() for line in file]
logger.debug(CATEGORIES)


name = "Uses an invalid category"
recommendation = f"Set $category to a category in {categories_txt} or exclude the package in the linter"
name = "Doesn't use the function VM-Get-Category"
recommendation = f"Set '$category = VM-Get-Category($MyInvocation.MyCommand.Definition)' or exclude the package in the linter"

def check(self, path):
if any([exclusion in str(path) for exclusion in self.EXCLUSIONS]):
return False

# utf-8-sig ignores BOM
file_content = open(path, "r", encoding="utf-8-sig").read()

match = re.search(r"\$category = ['\"](?P<category>[\w &/]+)['\"]", file_content)
if not match or match.group("category") not in self.CATEGORIES:
pattern = re.escape("$category = VM-Get-Category($MyInvocation.MyCommand.Definition)")
match = re.search(pattern, file_content)
if not match:
return True
return False


INSTALL_LINTS = (
MissesImportCommonVm(),
FirstLineDoesNotSetErrorAction(),
#This line has been disabled temporarily because it would validate the category from the chocolatey install script
#It needs to be disabled until a new linter checks if a valid category exists in the nuspec package
#UsesInvalidCategory(),
UsesCategoryFromNuspec(),
)

#UNINSTALL_LINTS = (UsesInvalidCategory(),)
UNINSTALL_LINTS = ()
UNINSTALL_LINTS = (UsesCategoryFromNuspec(),)


def lint_install(path):
Expand Down

0 comments on commit e161dfc

Please sign in to comment.