pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/pkglint pkglint: Update to 5.5.1



details:   https://anonhg.NetBSD.org/pkgsrc/rev/2c971528bd90
branches:  trunk
changeset: 374002:2c971528bd90
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Sat Jan 13 23:56:14 2018 +0000

description:
pkglint: Update to 5.5.1

Changes since 5.5:

* Allow filtering log messages by keyword (--only)
* In --autofix and --show-autofix mode, show only fixable diagnostics
* When called with --source, show the source below the diagnostics
* Don't warn about USE_LANGUAGES in ../../mk/compiler.mk
* Fix autofix for .gz in PLIST

diffstat:

 pkgtools/pkglint/DESCR                       |    6 +-
 pkgtools/pkglint/Makefile                    |    4 +-
 pkgtools/pkglint/files/autofix.go            |  238 +++++++++++++++++++
 pkgtools/pkglint/files/autofix_test.go       |  338 +++++++++++++++++++++++++++
 pkgtools/pkglint/files/buildlink3_test.go    |   32 +-
 pkgtools/pkglint/files/category.go           |   14 +-
 pkgtools/pkglint/files/check_test.go         |  107 +++++--
 pkgtools/pkglint/files/distinfo.go           |   13 +-
 pkgtools/pkglint/files/distinfo_test.go      |   16 +-
 pkgtools/pkglint/files/expecter.go           |    7 +-
 pkgtools/pkglint/files/files.go              |   49 ---
 pkgtools/pkglint/files/files_test.go         |   67 -----
 pkgtools/pkglint/files/getopt/getopt.go      |   29 ++
 pkgtools/pkglint/files/getopt/getopt_test.go |   31 ++
 pkgtools/pkglint/files/globaldata_test.go    |    8 +-
 pkgtools/pkglint/files/globalvars.go         |    7 +-
 pkgtools/pkglint/files/licenses_test.go      |    2 +-
 pkgtools/pkglint/files/line.go               |  188 +++++---------
 pkgtools/pkglint/files/line_test.go          |  110 --------
 pkgtools/pkglint/files/linechecker.go        |   29 +-
 pkgtools/pkglint/files/linechecker_test.go   |    6 +-
 pkgtools/pkglint/files/logging.go            |   71 ++++-
 pkgtools/pkglint/files/logging_test.go       |  140 +++++++++++
 pkgtools/pkglint/files/mkline.go             |    7 +-
 pkgtools/pkglint/files/mkline_test.go        |  127 ++++-----
 pkgtools/pkglint/files/mklinechecker.go      |   56 ++--
 pkgtools/pkglint/files/mklinechecker_test.go |   48 +-
 pkgtools/pkglint/files/mklines.go            |   51 ++-
 pkgtools/pkglint/files/mklines_test.go       |   54 ++--
 pkgtools/pkglint/files/mkparser.go           |    7 +-
 pkgtools/pkglint/files/mkparser_test.go      |    9 +-
 pkgtools/pkglint/files/package.go            |   30 +-
 pkgtools/pkglint/files/package_test.go       |   28 +-
 pkgtools/pkglint/files/patches.go            |   18 +-
 pkgtools/pkglint/files/patches_test.go       |   34 +-
 pkgtools/pkglint/files/pkglint.0             |   34 +-
 pkgtools/pkglint/files/pkglint.1             |   54 ++-
 pkgtools/pkglint/files/pkglint.go            |   25 +-
 pkgtools/pkglint/files/pkglint_test.go       |   18 +-
 pkgtools/pkglint/files/plist.go              |   47 ++-
 pkgtools/pkglint/files/plist_test.go         |   23 +-
 pkgtools/pkglint/files/shell_test.go         |  121 +++++----
 pkgtools/pkglint/files/substcontext_test.go  |    2 +-
 pkgtools/pkglint/files/vartypecheck.go       |   28 +-
 pkgtools/pkglint/files/vartypecheck_test.go  |    8 +-
 45 files changed, 1477 insertions(+), 864 deletions(-)

diffs (truncated from 4404 to 300 lines):

diff -r 676860928e5b -r 2c971528bd90 pkgtools/pkglint/DESCR
--- a/pkgtools/pkglint/DESCR    Sat Jan 13 23:18:25 2018 +0000
+++ b/pkgtools/pkglint/DESCR    Sat Jan 13 23:56:14 2018 +0000
@@ -1,6 +1,6 @@
-pkglint is for pkgsrc packages what lint(1) is for C files. It checks
-for various things that the used languages cannot detect, for example
-application-specific restrictions on certain variables.
+pkglint checks whether a pkgsrc package conforms to the various
+conventions established over the years. It produces warnings, errors and
+notes and, upon request, explains them.
 
 Before importing a new package or making changes to an existing package,
 pkglint should be run in the package's directory to check for common
diff -r 676860928e5b -r 2c971528bd90 pkgtools/pkglint/Makefile
--- a/pkgtools/pkglint/Makefile Sat Jan 13 23:18:25 2018 +0000
+++ b/pkgtools/pkglint/Makefile Sat Jan 13 23:56:14 2018 +0000
@@ -1,6 +1,6 @@
-# $NetBSD: Makefile,v 1.524 2018/01/10 00:39:52 rillig Exp $
+# $NetBSD: Makefile,v 1.525 2018/01/13 23:56:14 rillig Exp $
 
-PKGNAME=       pkglint-5.5
+PKGNAME=       pkglint-5.5.1
 DISTFILES=     # none
 CATEGORIES=    pkgtools
 
diff -r 676860928e5b -r 2c971528bd90 pkgtools/pkglint/files/autofix.go
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/pkgtools/pkglint/files/autofix.go Sat Jan 13 23:56:14 2018 +0000
@@ -0,0 +1,238 @@
+package main
+
+import (
+       "fmt"
+       "io/ioutil"
+       "netbsd.org/pkglint/regex"
+       "netbsd.org/pkglint/trace"
+       "os"
+       "strings"
+)
+
+// Autofix handles all modifications to a single line,
+// describes them in a human-readable form and formats the output.
+// The modifications are kept in memory only,
+// until they are written to disk by SaveAutofixChanges.
+type Autofix struct {
+       line        Line
+       linesBefore []string      // Newly inserted lines, including \n
+       lines       []*RawLine    // Original lines, available for diff
+       linesAfter  []string      // Newly inserted lines, including \n
+       modified    bool          // Modified in memory, but not necessarily written back to disk
+       descrFormat string        // Human-readable description of the latest modification
+       descrArgs   []interface{} //
+       level       *LogLevel     //
+       diagFormat  string        // Is printed only if it couldn't be fixed automatically
+       diagArgs    []interface{} //
+       explanation []string      // Is printed together with the diagnostic
+}
+
+func NewAutofix(line Line) *Autofix {
+       return &Autofix{
+               line:  line,
+               lines: append([]*RawLine{}, line.raw...)}
+}
+
+func (fix *Autofix) Replace(from string, to string) {
+       if fix.skip() {
+               return
+       }
+
+       for _, rawLine := range fix.lines {
+               if rawLine.Lineno != 0 {
+                       if replaced := strings.Replace(rawLine.textnl, from, to, 1); replaced != rawLine.textnl {
+                               if G.opts.PrintAutofix || G.opts.Autofix {
+                                       rawLine.textnl = replaced
+                               }
+                               fix.Describef("Replacing %q with %q.", from, to)
+                       }
+               }
+       }
+}
+
+func (fix *Autofix) ReplaceRegex(from regex.Pattern, to string) {
+       if fix.skip() {
+               return
+       }
+
+       for _, rawLine := range fix.lines {
+               if rawLine.Lineno != 0 {
+                       if replaced := regex.Compile(from).ReplaceAllString(rawLine.textnl, to); replaced != rawLine.textnl {
+                               if G.opts.PrintAutofix || G.opts.Autofix {
+                                       rawLine.textnl = replaced
+                               }
+                               fix.Describef("Replacing regular expression %q with %q.", from, to)
+                       }
+               }
+       }
+}
+
+func (fix *Autofix) InsertBefore(text string) {
+       if fix.skip() {
+               return
+       }
+
+       fix.linesBefore = append(fix.linesBefore, text+"\n")
+       fix.Describef("Inserting a line %q before this line.", text)
+}
+
+func (fix *Autofix) InsertAfter(text string) {
+       if fix.skip() {
+               return
+       }
+
+       fix.linesAfter = append(fix.linesAfter, text+"\n")
+       fix.Describef("Inserting a line %q after this line.", text)
+}
+
+func (fix *Autofix) Delete() {
+       if fix.skip() {
+               return
+       }
+
+       for _, line := range fix.lines {
+               line.textnl = ""
+       }
+       fix.Describef("Deleting this line.")
+}
+
+func (fix *Autofix) Describef(format string, args ...interface{}) {
+       fix.descrFormat = format
+       fix.descrArgs = args
+}
+
+func (fix *Autofix) Notef(format string, args ...interface{}) {
+       fix.level = llNote
+       fix.diagFormat = format
+       fix.diagArgs = args
+}
+
+func (fix *Autofix) Warnf(format string, args ...interface{}) {
+       fix.level = llWarn
+       fix.diagFormat = format
+       fix.diagArgs = args
+}
+
+func (fix *Autofix) Errorf(format string, args ...interface{}) {
+       fix.level = llError
+       fix.diagFormat = format
+       fix.diagArgs = args
+}
+
+func (fix *Autofix) Explain(explanation ...string) {
+       fix.explanation = explanation
+}
+
+// Depending on the pkglint mode, either:
+//
+// * logs the associated message (default)
+// * logs what would be fixed (--show-autofix)
+// * records the fixes in the line (--autofix)
+func (fix *Autofix) Apply() {
+       line := fix.line
+       if line.firstLine < 1 {
+               return
+       }
+
+       if shallBeLogged(fix.diagFormat) && fix.descrFormat != "" {
+               logDiagnostic := fix.level != nil && fix.diagFormat != "Silent-Magic-Diagnostic" && !G.opts.Autofix
+               if logDiagnostic {
+                       msg := fmt.Sprintf(fix.diagFormat, fix.diagArgs...)
+                       logs(fix.level, line.Filename, line.Linenos(), fix.diagFormat, msg)
+               }
+
+               logRepair := G.opts.Autofix || G.opts.PrintAutofix
+               if logRepair {
+                       msg := fmt.Sprintf(fix.descrFormat, fix.descrArgs...)
+                       logs(llAutofix, line.Filename, line.Linenos(), "", msg)
+               }
+
+               if logDiagnostic || logRepair {
+                       line.printSource(G.logOut)
+                       if G.opts.Explain && logDiagnostic && len(fix.explanation) != 0 {
+                               Explain(fix.explanation...)
+                       } else if G.opts.PrintSource {
+                               G.logOut.Separate()
+                       }
+               }
+       }
+
+       fix.modified = fix.modified || fix.descrFormat != ""
+
+       fix.descrFormat = ""
+       fix.descrArgs = nil
+       fix.level = nil
+       fix.diagFormat = ""
+       fix.diagArgs = nil
+       fix.explanation = nil
+}
+
+func (fix *Autofix) skip() bool {
+       if fix.diagFormat == "" {
+               panic("Autofix: The diagnostic must be given before the action.")
+       }
+       return !shallBeLogged(fix.diagFormat)
+}
+
+// SaveAutofixChanges writes the given lines back into their files,
+// applying the autofix changes.
+// The lines may come from different files.
+// Only files that actually have changed lines are saved.
+func SaveAutofixChanges(lines []Line) (autofixed bool) {
+       if trace.Tracing {
+               defer trace.Call0()()
+       }
+
+       if !G.opts.Autofix {
+               for _, line := range lines {
+                       if line.autofix != nil && line.autofix.modified {
+                               G.autofixAvailable = true
+                       }
+               }
+               return
+       }
+
+       changes := make(map[string][]string)
+       changed := make(map[string]bool)
+       for _, line := range lines {
+               chlines := changes[line.Filename]
+               if fix := line.autofix; fix != nil {
+                       if fix.modified {
+                               changed[line.Filename] = true
+                       }
+                       chlines = append(chlines, fix.linesBefore...)
+                       for _, raw := range line.raw {
+                               chlines = append(chlines, raw.textnl)
+                       }
+                       chlines = append(chlines, fix.linesAfter...)
+               } else {
+                       for _, raw := range line.raw {
+                               chlines = append(chlines, raw.textnl)
+                       }
+               }
+               changes[line.Filename] = chlines
+       }
+
+       for fname := range changed {
+               changedLines := changes[fname]
+               tmpname := fname + ".pkglint.tmp"
+               text := ""
+               for _, changedLine := range changedLines {
+                       text += changedLine
+               }
+               err := ioutil.WriteFile(tmpname, []byte(text), 0666)
+               if err != nil {
+                       NewLineWhole(tmpname).Errorf("Cannot write.")
+                       continue
+               }
+               err = os.Rename(tmpname, fname)
+               if err != nil {
+                       NewLineWhole(fname).Errorf("Cannot overwrite with auto-fixed content.")
+                       continue
+               }
+               msg := "Has been auto-fixed. Please re-run pkglint."
+               logs(llAutofix, fname, "", msg, msg)
+               autofixed = true
+       }
+       return
+}
diff -r 676860928e5b -r 2c971528bd90 pkgtools/pkglint/files/autofix_test.go
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/pkgtools/pkglint/files/autofix_test.go    Sat Jan 13 23:56:14 2018 +0000
@@ -0,0 +1,338 @@
+package main
+
+import "gopkg.in/check.v1"
+
+func (s *Suite) Test_Autofix_ReplaceRegex(c *check.C) {
+       s.Init(c)
+       s.UseCommandLine("--show-autofix")
+       fname := s.CreateTmpFile("Makefile", ""+
+               "line1\n"+
+               "line2\n"+
+               "line3\n")
+       lines := LoadExistingLines(fname, true)
+
+       fix := lines[1].Autofix()
+       fix.Warnf("Something's wrong here.")
+       fix.ReplaceRegex(`.`, "X")
+       fix.Apply()
+       SaveAutofixChanges(lines)
+
+       c.Check(lines[1].raw[0].textnl, equals, "XXXXX\n")
+       c.Check(s.LoadTmpFile("Makefile"), equals, "line1\nline2\nline3\n")
+       s.CheckOutputLines(
+               "WARN: ~/Makefile:2: Something's wrong here.",
+               "AUTOFIX: ~/Makefile:2: Replacing regular expression \".\" with \"X\".")
+}
+
+func (s *Suite) Test_Autofix_ReplaceRegex_with_autofix(c *check.C) {
+       s.Init(c)
+       s.UseCommandLine("--autofix", "--source")



Home | Main Index | Thread Index | Old Index