Skip to content

Commit

Permalink
Fixes #21: Failed to recognize go mod updates needed. (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Bonic authored Dec 31, 2021
1 parent 27b2be7 commit c283d43
Show file tree
Hide file tree
Showing 10 changed files with 1,357 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .gotestfmt/downloads.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This template contains the format for a package download.
*/ -}}
{{- $settings := .Settings -}}
{{- if .Packages -}}
{{- if or .Packages .Reason -}}
{{- if or (not $settings.HideSuccessfulDownloads) .Failed -}}
{{- if .Failed -}}
{{ "\033" }}[0;31m❌
Expand Down Expand Up @@ -31,4 +31,7 @@ This template contains the format for a package download.
{{- end -}}
{{- end -}}
{{- end -}}
{{- with .Reason -}}
{{- " " -}}{{- "\033" }}[0;31m🛑 {{ . }}{{- "\033" }}[0m{{ "\n" -}}
{{- end -}}
{{- end -}}
7 changes: 5 additions & 2 deletions .gotestfmt/github/downloads.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This template contains the format for a package download.
*/ -}}
{{- $settings := .Settings -}}
{{- if .Packages -}}
{{- if or .Packages .Reason -}}
{{- if or (not .Settings.HideSuccessfulDownloads) .Failed -}}
::group::
{{- if .Failed -}}
Expand Down Expand Up @@ -31,6 +31,9 @@ This template contains the format for a package download.
{{- end -}}
{{- end -}}
{{- end -}}
{{- with .Reason -}}
{{- " " -}}{{- "\033" }}[0;31m🛑 {{ . }}{{- "\033" }}[0m{{ "\n" -}}
{{- end -}}
::endgroup::
{{- end -}}
{{- end -}}
{{- end -}}
7 changes: 5 additions & 2 deletions .gotestfmt/gitlab/downloads.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This template contains the format for a package download.
*/ -}}
{{- $settings := .Settings -}}
{{- if .Packages -}}
{{- if or .Packages .Reason -}}
{{- if or (not .Settings.HideSuccessfulDownloads) .Failed -}}
{{- "\033" }}[0Ksection_start:{{ with .StartTime }}{{ .Unix }}{{ else }}0{{ end }}:dependency_downloads{{ "\r\033" }}[0K{{- "\n" -}}
{{- if .Failed -}}
Expand Down Expand Up @@ -31,6 +31,9 @@ This template contains the format for a package download.
{{- end -}}
{{- end -}}
{{- end -}}
{{- "\033" }}[0Ksection_start:{{ with .EndTime }}{{ .Unix }}{{ else }}0{{ end }}:dependency_downloads{{ "\r\033" }}[0K{{- "\n" -}}
{{- with .Reason -}}
{{- " " -}}{{- "\033" }}[0;31m🛑 {{ . }}{{- "\033" }}[0m{{ "\n" -}}
{{- end -}}
{{- "\033" }}[0Ksection_end:{{ with .EndTime }}{{ .Unix }}{{ else }}0{{ end }}:dependency_downloads{{ "\r\033" }}[0K{{- "\n" -}}
{{- end -}}
{{- end -}}
8 changes: 5 additions & 3 deletions .gotestfmt/teamcity/downloads.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
This template contains the format for a package download.
*/ -}}
{{- $settings := .Settings -}}
{{- if .Packages -}}
{{- if or .Packages .Reason -}}
{{- if or (not $settings.HideSuccessfulDownloads) .Failed -}}
{{- $title := "Dependency downloads" -}}
{{- if .Failed -}}
{{- $title = print "❌ " $title -}}
{{- else -}}
{{- $title = print "📥 " $title -}}
{{- end -}}
##teamcity[blockOpened name='{{ $title }}']

##teamcity[blockOpened name='{{ $title }}']{{ "\n" -}}
{{- range .Packages -}}
{{- if or (not $settings.HideSuccessfulDownloads) .Failed -}}
{{- " " -}}
Expand All @@ -29,6 +28,9 @@ This template contains the format for a package download.
{{- end -}}
{{- end -}}
{{- end -}}
{{- with .Reason -}}
{{- " " -}}🛑 {{ . }}{{ "\n" -}}
{{- end -}}
##teamcity[blockClosed name='{{ $title }}']
{{- end -}}
{{- end -}}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ This file contains the output fragment showing the package downloads in the Go t
| `.Packages` | `[]Package` | A list of packages that have been processed. |
| `.StartTime` | `*time.Time` | The time the first download line was seen. May be empty. |
| `.EndTime` | `*time.Time` | The time the last download line was seen. May be empty. |
| `.Reason` | `string` | If an extra reason is given for the failure, the text is included here. |
| `.Settings` | [`RenderSettings`](#render-settings) | The render settings (what to hide, etc, [see below](#render-settings)). |

The `Package` items have the following format:
Expand Down
3 changes: 3 additions & 0 deletions parser/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (p *Package) ID() string {
)
}

// Download is the download information about a single package.
type Download struct {
// Package is the name of the package being downloaded.
Package string `json:"package"`
Expand All @@ -111,6 +112,8 @@ type Downloads struct {
StartTime *time.Time `json:"-"`
// EndTime indicates when the downloads finished.
EndTime *time.Time `json:"-"`
// Reason describes the failure reason if a separate one is present.
Reason string `json:"reason"`
}

// ParseResult is an overall structure for parser results, containing the prefix text, downloads and packagesByName.
Expand Down
64 changes: 45 additions & 19 deletions parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,32 @@ func Parse(
) (<-chan string, <-chan *Downloads, <-chan *Package) {
prefixChannel := make(chan string)
downloadsChannel := make(chan *Downloads)
downloadsFailureReason := make(chan string)
packagesChannel := make(chan *Package)
go parse(evts, prefixChannel, downloadsChannel, packagesChannel)
go parse(evts, prefixChannel, downloadsChannel, downloadsFailureReason, packagesChannel)
return prefixChannel, downloadsChannel, packagesChannel
}

var downloadErrors = []*regexp.Regexp{
regexp.MustCompile(`no required module provides package (?P<package>[^\s]+);`),
regexp.MustCompile(`updates to go.mod needed; to update it:`),
}

func parse(
evts <-chan tokenizer.Event,
prefixChannel chan string,
downloadsChannel chan *Downloads,
downloadsFailureReason chan string,
packagesChannel chan *Package,
) {
outputStarted := false
downloadTracker := &downloadsTracker{
prefixChannel: prefixChannel,
downloadResultsList: nil,
downloadsByPackage: map[string]*Download{},
downloadsFinished: false,
target: downloadsChannel,
prefixChannel: prefixChannel,
downloadResultsList: nil,
downloadsByPackage: map[string]*Download{},
downloadsFinished: false,
downloadsFailureReason: downloadsFailureReason,
target: downloadsChannel,
}
pkgTracker := &packageTracker{
packagesByName: map[string]*Package{},
Expand Down Expand Up @@ -131,17 +135,26 @@ func parse(
foundDLError := false
for _, dlError := range downloadErrors {
if submatch := dlError.FindSubmatch(evt.Output); len(submatch) > 0 {
pkgName := string(submatch[1])
downloadTracker.SetDownloadFailed(pkgName, "")
downloadTracker.AddReason(pkgName, evt.Output)
prevErroredDownload = pkgName
if len(submatch) > 1 {
pkgName := string(submatch[1])
downloadTracker.SetDownloadFailed(pkgName, "")
downloadTracker.AddReason(pkgName, evt.Output)
prevErroredDownload = pkgName
} else {
downloadTracker.SetFailureReason(submatch[0])
prevErroredDownload = "*"
}
foundDLError = true
outputStarted = true
}
}
if !foundDLError {
if prevErroredDownload != "" {
downloadTracker.AddReason(prevErroredDownload, evt.Output)
if prevErroredDownload == "*" {
downloadTracker.SetFailureReason(evt.Output)
} else {
downloadTracker.AddReason(prevErroredDownload, evt.Output)
}
} else if prevErroredPkg != "" {
pkgTracker.AddOutput(prevErroredPkg, "", evt.Output)
} else if !outputStarted {
Expand Down Expand Up @@ -341,14 +354,16 @@ func compareTestCaseNames(name1 string, name2 string) bool {
}

type downloadsTracker struct {
downloadResultsList []*Download
downloadsByPackage map[string]*Download
downloadsFinished bool
lastDownload *Download
target chan *Downloads
prefixChannel chan string
startTime *time.Time
endTime *time.Time
downloadResultsList []*Download
downloadsByPackage map[string]*Download
downloadsFinished bool
lastDownload *Download
target chan *Downloads
prefixChannel chan string
startTime *time.Time
endTime *time.Time
downloadsFailureReason chan string
failureReason []byte
}

func (d *downloadsTracker) Add(name string, version string) {
Expand Down Expand Up @@ -379,11 +394,15 @@ func (d *downloadsTracker) Write() {
}
dl.Reason = strings.TrimRight(dl.Reason, "\n")
}
if len(d.failureReason) > 0 {
failed = true
}
d.target <- &Downloads{
Packages: d.downloadResultsList,
Failed: failed,
StartTime: d.startTime,
EndTime: d.endTime,
Reason: strings.TrimSpace(string(d.failureReason)),
}
d.downloadsFinished = true
d.downloadResultsList = nil
Expand Down Expand Up @@ -424,3 +443,10 @@ func (d *downloadsTracker) AddReason(name string, output []byte) {
pkg := d.ensurePackage(name)
pkg.Reason = pkg.Reason + string(output) + "\n"
}

func (d *downloadsTracker) SetFailureReason(output []byte) {
if d.downloadsFinished {
panic(fmt.Errorf("tried to add download failure reason after downloads are already finished"))
}
d.failureReason = append(append(d.failureReason, output...), '\n')
}
Loading

0 comments on commit c283d43

Please sign in to comment.