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

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

Revision 1.31, Sun Dec 8 00:06:38 2019 UTC (2 months, 2 weeks ago) by rillig
Branch: MAIN
CVS Tags: pkgsrc-2019Q4-base, pkgsrc-2019Q4
Changes since 1.30: +14 -0 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_Load(c *check.C) {
	t := s.Init(c)

	nonexistent := t.File("nonexistent")
	empty := t.CreateFileLines("empty")
	oneLiner := t.CreateFileLines("one-liner",
		"hello, world")

	t.Check(Load(nonexistent, 0), check.IsNil)
	t.Check(Load(empty, 0).Lines, check.HasLen, 0)
	t.CheckEquals(Load(oneLiner, 0).Lines[0].Text, "hello, world")

	t.CheckOutputEmpty()

	t.Check(Load(nonexistent, LogErrors), check.IsNil)
	t.Check(Load(empty, LogErrors).Lines, check.HasLen, 0)
	t.CheckEquals(Load(oneLiner, LogErrors).Lines[0].Text, "hello, world")

	t.CheckOutputLines(
		"ERROR: ~/nonexistent: Cannot be read.")

	t.Check(Load(nonexistent, NotEmpty), check.IsNil)
	t.Check(Load(empty, NotEmpty), check.IsNil)
	t.CheckEquals(Load(oneLiner, NotEmpty).Lines[0].Text, "hello, world")

	t.CheckOutputEmpty()

	t.Check(Load(nonexistent, NotEmpty|LogErrors), check.IsNil)
	t.Check(Load(empty, NotEmpty|LogErrors), check.IsNil)
	t.CheckEquals(Load(oneLiner, NotEmpty|LogErrors).Lines[0].Text, "hello, world")

	t.CheckOutputLines(
		"ERROR: ~/nonexistent: Cannot be read.",
		"ERROR: ~/empty: Must not be empty.")

	t.ExpectFatal(
		func() { Load(t.File("does-not-exist"), MustSucceed) },
		"FATAL: ~/does-not-exist: Cannot be read.")

	t.ExpectFatal(
		func() { Load(t.File("empty"), MustSucceed|NotEmpty) },
		"FATAL: ~/empty: Must not be empty.")
}

// Up to 2019-12-04, pkglint suppressed fatal errors when it was started
// with the --autofix option. This was another case where the clear
// separation between diagnostics and technical errors had been confused.
func (s *Suite) Test_Load__not_found_in_autofix_mode(c *check.C) {
	t := s.Init(c)

	t.SetUpCommandLine("--autofix")
	t.Chdir(".")

	t.ExpectFatal(
		func() { Load("nonexistent", MustSucceed) },
		"FATAL: nonexistent: Cannot be read.")
}

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

	rawText := "" +
		"first line\n" +
		"second line\n"

	lines := convertToLogicalLines("filename", rawText, false)

	t.CheckEquals(lines.Len(), 2)
	t.CheckEquals(lines.Lines[0].String(), "filename:1: first line")
	t.CheckEquals(lines.Lines[1].String(), "filename:2: second line")
}

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

	rawText := "" +
		"first line, \\\n" +
		"still first line\n" +
		"second line\n"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Len(), 2)
	t.CheckEquals(lines.Lines[0].String(), "filename:1--2: first line, still first line")
	t.CheckEquals(lines.Lines[1].String(), "filename:3: second line")
}

// This test demonstrates that pkglint deviates from bmake.
// Bmake keeps all the trailing whitespace from the line and replaces the
// backslash plus any indentation with a single space. This results in:
//  "\tprintf '%s\\n' 'none none  space  space  tab\t tab'"
// This is another of the edge cases probably no-one relies on.
func (s *Suite) Test_convertToLogicalLines__continuation_spacing(c *check.C) {
	t := s.Init(c)

	rawText := "" +
		"\tprintf '%s\\n' 'none\\\n" +
		"none\\\n" +
		"space \\\n" +
		" space \\\n" +
		"tab\t\\\n" +
		"\ttab'\n"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Lines[0].Text,
		"\tprintf '%s\\n' 'none none space space tab tab'")
}

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

	rawText := "" +
		"last line\\\n"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Len(), 1)
	t.CheckEquals(lines.Lines[0].String(), "filename:1: last line\\")
	t.CheckOutputEmpty()
}

// In Makefiles, comment lines can also have continuations.
// See devel/bmake/files/unit-tests/comment.mk
func (s *Suite) Test_convertToLogicalLines__comments(c *check.C) {
	t := s.Init(c)

	mklines := t.SetUpFileMkLines("comment.mk",
		"# This is a comment",
		"",
		"#\\",
		"\tMultiline comment",
		"# Another escaped comment \\",
		"that \\",
		"goes \\",
		"on and on",
		"# This is NOT an escaped comment due to the double backslashes \\\\",
		"VAR=\tThis is not a comment",
		"",
		"#\\",
		"\tThis is a comment",
		"#\\\\",
		"\tThis is no comment",
		"#\\\\\\",
		"\tThis is a comment",
		"#\\\\\\\\",
		"\tThis is no comment",
		"#\\\\\\\\\\",
		"\tThis is a comment",
		"#\\\\\\\\\\\\",
		"\tThis is no comment")

	var texts []string
	for _, line := range mklines.lines.Lines {
		texts = append(texts, line.Text)
	}

	t.CheckDeepEquals(texts, []string{
		"# This is a comment",
		"",
		"# Multiline comment",
		"# Another escaped comment that goes on and on",
		"# This is NOT an escaped comment due to the double backslashes \\",
		"VAR=\tThis is not a comment",
		"",
		"# This is a comment",
		"#\\",
		"\tThis is no comment",
		"#\\ This is a comment",
		"#\\\\",
		"\tThis is no comment",
		"#\\\\ This is a comment",
		"#\\\\\\",
		"\tThis is no comment"})

	t.CheckOutputEmpty()
}

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

	rawText := "" +
		"The package description\n" +
		"takes 2 lines"

	lines := convertToLogicalLines("DESCR", rawText, true)

	t.CheckEquals(lines.Len(), 2)
	t.CheckEquals(lines.Lines[1].String(), "DESCR:2: takes 2 lines")
	t.CheckOutputLines(
		"ERROR: DESCR:2: File must end with a newline.")
}

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

	rawText := "" +
		"last line\\"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Len(), 1)
	t.CheckEquals(lines.Lines[0].String(), "filename:1: last line\\")
	t.CheckOutputLines(
		"ERROR: filename:1: File must end with a newline.")
}

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

	t.SetUpCommandLine("-Wall", "--source")
	rawText := "" +
		"last line\\"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Len(), 1)
	t.CheckEquals(lines.Lines[0].String(), "filename:1: last line\\")
	t.CheckOutputLines(
		">\tlast line\\",
		"ERROR: filename:1: File must end with a newline.")
}

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

	mklines := t.NewMkLines("filename.mk",
		"#COMMENTED= \\",
		"#\tcontinuation 1 \\",
		"#\tcontinuation 2")
	mkline := mklines.mklines[0]

	// The leading comments are stripped from the continuation lines as well.
	t.CheckEquals(mkline.Value(), "continuation 1 \tcontinuation 2")
	t.CheckEquals(mkline.HasComment(), false)
}

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

	leadingWhitespace, text, trailingWhitespace, continuation := matchContinuationLine("\n")

	t.CheckEquals(leadingWhitespace, "")
	t.CheckEquals(text, "")
	t.CheckEquals(trailingWhitespace, "")
	t.CheckEquals(continuation, "")

	leadingWhitespace, text, trailingWhitespace, continuation = matchContinuationLine("\tword   \\\n")

	t.CheckEquals(leadingWhitespace, "\t")
	t.CheckEquals(text, "word")
	t.CheckEquals(trailingWhitespace, "   ")
	t.CheckEquals(continuation, "\\")
}