[BACK]Return to vartype_test.go CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / pkgsrc / pkgtools / pkglint / files

File: [cvs.NetBSD.org] / pkgsrc / pkgtools / pkglint / files / vartype_test.go (download)

Revision 1.23, Sun Dec 8 00:06:38 2019 UTC (2 months, 3 weeks ago) by rillig
Branch: MAIN
Changes since 1.22: +77 -4 lines

pkgtools/pkglint: update to 19.3.14

Changes since 19.3.13:

When pkglint suggests to replace !empty(VARNAME:Mfixed) with ${VARNAME}
== fixed, the exact suggested expression is now part of the diagnostic.
The check and the autofix have been improved. They now apply only to the
last modifier in the whole chain, everything else was a bug in pkglint.

Pkglint now knows the scope of variables better than before. It knows
the difference between variables from <sys.mk> like MACHINE_ARCH, which
are always in scope, and those from mk/defaults/mk.conf, which only come
into scope later, after bsd.prefs.mk has been included. It warns when
variables are used too early, for example in .if conditions.

The pathnames in ALTERNATIVES files are now checked for absolute
pathnames. This mistake doesn't happen in practice, but the code for
converting the different path types internally made it necessary to add
these checks. At least this prevents typos.

The special check for obsolete licenses has been removed since their
license files have been removed and that is checked as well.

Variables named *_AWK may be appended to.

The variables _PKG_SILENT and _PKG_DEBUG are no longer deprecated, they
are obsolete now. They are not used in main pkgsrc and pkgsrc-wip
anymore.

When a package sets a default value for a user-settable variable (which
is something that should not happen anyway), it should .include
bsd.prefs.mk before, in order to not accidentally overwrite the
user-specified value.

Variable modifiers of the form :from=to are now parsed like in bmake.
They are greedy and eat up any following colons as well. This means that
${VAR:.c=.o:Q} replaces source.c with source.o:Q, instead of quoting it.
Pkglint now warns about such cases.

The handling of relative paths in diagnostics is now consistent. All
paths that are part of a diagnostic are relative to the line that issues
the diagnostic.

Fatal errors are no longer suppressed in --autofix mode.

Plus lots of refactoring, to prevent accidental mixing of incompatible
relative paths.

package pkglint

import (
	"gopkg.in/check.v1"
)

func (s *Suite) Test_ACLPermissions_Contains(c *check.C) {
	t := s.Init(c)

	perms := aclpAllRuntime

	t.CheckEquals(perms.Contains(aclpAllRuntime), true)
	t.CheckEquals(perms.Contains(aclpUse), true)
	t.CheckEquals(perms.Contains(aclpUseLoadtime), false)
}

func (s *Suite) Test_ACLPermissions_String(c *check.C) {
	t := s.Init(c)

	t.CheckEquals(ACLPermissions(0).String(), "none")
	t.CheckEquals(aclpAll.String(), "set, set-default, append, use-loadtime, use")
}

func (s *Suite) Test_ACLPermissions_HumanString(c *check.C) {
	t := s.Init(c)

	// Doesn't happen in practice
	t.CheckEquals(ACLPermissions(0).HumanString(), "")

	t.CheckEquals(
		aclpAll.HumanString(),
		"set, given a default value, appended to, used at load time, or used")
}

func (s *Suite) Test_Vartype_EffectivePermissions(c *check.C) {
	t := s.Init(c)

	t.SetUpVartypes()

	if typ := G.Pkgsrc.vartypes.Canon("PREFIX"); c.Check(typ, check.NotNil) {
		t.CheckEquals(typ.basicType.name, "Pathname")
		t.CheckDeepEquals(typ.aclEntries, []ACLEntry{NewACLEntry("*", aclpUse)})
		t.CheckEquals(typ.EffectivePermissions("Makefile"), aclpUse)
		t.CheckEquals(typ.EffectivePermissions("buildlink3.mk"), aclpUse)
	}

	if typ := G.Pkgsrc.vartypes.Canon("EXTRACT_OPTS"); c.Check(typ, check.NotNil) {
		t.CheckEquals(typ.basicType.name, "ShellWord")
		t.CheckEquals(typ.EffectivePermissions("Makefile"), aclpAllWrite|aclpUse)
		t.CheckEquals(typ.EffectivePermissions("options.mk"), aclpAllWrite|aclpUse)
	}
}

func (s *Suite) Test_Vartype_AlternativeFiles(c *check.C) {
	t := s.Init(c)

	// test generates the files description for the "set" permission.
	test := func(rules []string, alternatives string) {
		aclEntries := (*VarTypeRegistry).parseACLEntries(nil, "", rules...)
		vartype := NewVartype(BtYesNo, NoVartypeOptions, aclEntries...)

		alternativeFiles := vartype.AlternativeFiles(aclpSet)

		t.CheckEquals(alternativeFiles, alternatives)
	}

	// rules parses the given permission rules.
	//
	// There is a built-in check that prevents repeated adjacent permissions.
	// The "append" permission can be added alternatively to circumvent this
	// check, since that permission is effectively ignore by this test.
	rules := func(rules ...string) []string { return rules }

	// When there are no matching rules at all, there's nothing to describe.
	test(
		rules(
			"*: none"),
		"")

	// When there are only positive rules that are disjoint, these are
	// listed in the given order.
	test(
		rules(
			"buildlink3.mk: set",
			"Makefile: set, append", // to avoid "repeated permissions" panic
			"Makefile.*: set"),
		"buildlink3.mk, Makefile or Makefile.*")

	// When there are only positive rules and some of them overlap,
	// these are merged.
	test(
		rules(
			"buildlink3.mk: set",
			"special:*3.mk: set, append",
			"*.mk: set",
			"Makefile: set, append",
			"Makefile.*: set"),
		"*.mk, Makefile or Makefile.*")

	// When the last rule is "*", all previous rules are merged into that.
	test(
		rules(
			"buildlink3.mk: set",
			"special:*3.mk: set, append",
			"*.mk: set",
			"Makefile: set, append",
			"Makefile.*: set",
			"*: set, append"),
		"*")

	test(
		rules(
			"buildlink3.mk: set",
			"*: none"),
		"buildlink3.mk only")

	// Everywhere except in buildlink3.mk.
	test(
		rules(
			"buildlink3.mk: none",
			"*: set"),
		// TODO: should be "buildlink3.mk only".
		"*, but not buildlink3.mk")

	// If there are both positive and negative cases, preserve all the
	// rules verbatim. Otherwise it would be too confusing for the
	// pkglint users to see and maybe learn the actual rules.
	test(
		rules(
			"buildlink3.mk: none",
			"special:*3.mk: set",
			"*.mk: none",
			"Makefile: set",
			"Makefile.*: none",
			"*: set"),
		"*3.mk, Makefile or *, but not buildlink3.mk, *.mk or Makefile.*")

	test(
		rules(
			"buildlink3.mk: none",
			"builtin.mk: set",
			"Makefile: none",
			"*.mk: append"),
		// TODO: should be "builtin.mk only".
		"builtin.mk, but not buildlink3.mk, Makefile or *.mk")
}

func (s *Suite) Test_Vartype_MayBeAppendedTo(c *check.C) {
	t := s.Init(c)

	t.SetUpVartypes()

	test := func(varname string, isAppendAllowed bool) {
		vartype := G.Pkgsrc.VariableType(nil, varname)

		t.CheckEquals(vartype.MayBeAppendedTo(), isAppendAllowed)
	}

	// There are several packages that append a parenthesized addition
	// to the comment, such as "(command-line version)".
	test("COMMENT", true)

	// Appending to a list is always ok.
	test("DEPENDS", true)
	test("PKG_FAIL_REASON", true)

	// This type is not marked as a list since it does not support
	// appending a single element, therefore the above rule does not apply.
	// Whenever something is appended, it must be in pairs of two words.
	test("CONF_FILES", true)

	// By convention, all variables ending in _AWK contain AWK code.
	// It is usual to append a single rule to it, such as:
	//  EXAMPLE_AWK+=   /condition/ { action }
	test("EXAMPLE_AWK", true)

	// This is another variable where the appended things should always
	// come in pairs. A typical example is:
	//  SUBST_SED.id+=  -e s,from,to,
	test("SUBST_SED.id", true)
}

func (s *Suite) Test_Vartype_String(c *check.C) {
	t := s.Init(c)

	t.SetUpVartypes()

	vartype := G.Pkgsrc.VariableType(nil, "PKG_DEBUG_LEVEL")
	t.CheckEquals(vartype.String(), "Integer (command-line-provided)")
}

func (s *Suite) Test_BasicType_NeedsQ(c *check.C) {
	t := s.Init(c)

	t.SetUpVartypes()

	test := func(varname string, isAppendAllowed bool) {
		vartype := G.Pkgsrc.VariableType(nil, varname)

		t.CheckEquals(vartype.basicType.NeedsQ(), isAppendAllowed)
	}

	test("BUILDLINK_DEPMETHOD.pkgbase", false)
	test("CATEGORIES", false)
	test("EXTRACT_SUFX", false)
	test("EMUL_PLATFORM", false)
	test("BINMODE", false)

	// Typically safe, seldom used in practice.
	test("DISTFILES", false)

	// XXX: BtIdentifier is used for several other purposes
	test("PLIST_VARS", false)

	test("MAKE_JOBS", false) // XXX: What if MAKE_JOBS is undefined?
	test("PKGREVISION", false)

	// A specific platform does not have special characters.
	// The patterns for such platforms typically do, such as
	// x86_64-*-*.
	test("MACHINE_GNU_PLATFORM", false)

	// A specific platform does not have special characters.
	// The patterns for such platforms typically do, such as
	// NetBSD-*-*.
	test("MACHINE_PLATFORM", false)

	test("PERL5_PACKLIST", false)
	test("PKG_OPTIONS_VAR", false)
	test("PYTHON_VERSIONED_DEPENDENCIES", false)
	test("SUBST_STAGE.id", false)
	test("IS_BUILTIN.pkgbase", false)

	test("COMMENT", true)
	test("PKG_FAIL_REASON", true)
	test("SUBST_MESSAGE.id", true)
	test("CC", true)
}

func (s *Suite) Test_BasicType_HasEnum(c *check.C) {
	t := s.Init(c)

	vc := enum("start middle end")

	t.CheckEquals(vc.HasEnum("start"), true)
	t.CheckEquals(vc.HasEnum("middle"), true)
	t.CheckEquals(vc.HasEnum("end"), true)

	t.CheckEquals(vc.HasEnum("star"), false)
	t.CheckEquals(vc.HasEnum("mid"), false)
	t.CheckEquals(vc.HasEnum("nd"), false)
	t.CheckEquals(vc.HasEnum("start middle"), false)
}