github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/cmd/tast-lint/internal/check/external_json.go (about)

     1  // Copyright 2019 The ChromiumOS Authors
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package check
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"go/token"
    11  	"path/filepath"
    12  	"regexp"
    13  	"strings"
    14  	"time"
    15  )
    16  
    17  // dateSuffixRe is a compiled regular expression for date suffix check.
    18  // The filename stem should end with YYYYMMDD date pattern, or in addition
    19  // _RC\d+ pattern version suffix.
    20  var dateSuffixRe = regexp.MustCompile(`(?:[-_])(\d{8})(?:_RC\d+)?(?:[-_.]|$)`)
    21  
    22  // ExternalJSON checks if url in .external file has a date suffix or not.
    23  func ExternalJSON(path string, in []byte) []*Issue {
    24  	type DataFile struct {
    25  		URL string `json:"url"`
    26  	}
    27  	var dataFile DataFile
    28  	if err := json.Unmarshal(in, &dataFile); err != nil {
    29  		return nil
    30  	}
    31  	url := dataFile.URL
    32  	if !strings.HasPrefix(url, "gs://chromiumos-test-assets-public/tast/") && !strings.HasPrefix(url, "gs://chromeos-test-assets-private/tast/") {
    33  		return nil
    34  	}
    35  
    36  	// Ignore crostini because it manages version with some numbers, not the date suffix.
    37  	crostiniPrefixList := []string{
    38  		"gs://chromiumos-test-assets-public/tast/cros/vm/termina_kernel_aarch64_",
    39  		"gs://chromiumos-test-assets-public/tast/cros/vm/termina_kernel_x86_64_",
    40  		"gs://chromiumos-test-assets-public/tast/cros/vm/termina_rootfs_aarch64_",
    41  		"gs://chromiumos-test-assets-public/tast/cros/vm/termina_rootfs_x86_64_",
    42  		"gs://chromeos-test-assets-private/tast/crosint/graphics/traces/",
    43  	}
    44  	for _, prefix := range crostiniPrefixList {
    45  		if strings.HasPrefix(url, prefix) {
    46  			return nil
    47  		}
    48  	}
    49  
    50  	base := filepath.Base(url)
    51  	match := dateSuffixRe.FindStringSubmatch(base)
    52  	if match != nil {
    53  		if _, err := time.Parse("20060102", match[1]); err == nil {
    54  			return nil
    55  		}
    56  	}
    57  
    58  	ext := filepath.Ext(base)
    59  	return []*Issue{{
    60  		Pos:  token.Position{Filename: path},
    61  		Msg:  fmt.Sprintf("include the date as a suffix in the filename like \".../%s_YYYYMMDD%s\"", strings.TrimRight(base, ext), ext),
    62  		Link: "https://chromium.googlesource.com/chromiumos/platform/tast/+/HEAD/docs/writing_tests.md#External-data-files",
    63  	}}
    64  }