Repository: googleapis/api-linter
Pull request #75 (2019-05-23 15:04:23)

Update README.md


Patch:
diff --git a/README.md b/README.md
index dc40103..ae87dfe 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@ API linter checks APIs defined in protobuf files. It follows [Google API Design
 * Install `api-linter` using `go get`:
 
 ```sh
-go get github.com/googleapis/api-linter/cmd/api-linter
+go get -u github.com/googleapis/api-linter/cmd/api-linter
 ```
 
 This installs `api-linter` into your local Go binary folder `$HOME/go/bin`. Ensure that your operating system's `PATH` contains the folder.
diff --git a/lint/config.go b/lint/config.go
index 9b5bdb7..cd2da3e 100644
--- a/lint/config.go
+++ b/lint/config.go
@@ -109,9 +109,9 @@ func ReadConfigsYAML(f io.Reader) (Configs, error) {
 	return c, nil
 }
 
-// getRuleConfig returns a RuleConfig that matches the given path and rule.
+// GetRuleConfig returns a RuleConfig that matches the given path and rule.
 // Returns an error if a config is not found for the path.
-func (c Configs) getRuleConfig(path string, rule RuleName) (result RuleConfig, err error) {
+func (c Configs) GetRuleConfig(path string, rule RuleName) (result RuleConfig, err error) {
 	err = fmt.Errorf("failed to find a config for path %q", path)
 	for _, cfg := range c {
 		if cfg.match(path) {
diff --git a/lint/linter.go b/lint/linter.go
index d698cf7..9dc37ec 100644
--- a/lint/linter.go
+++ b/lint/linter.go
@@ -73,7 +73,7 @@ func (l *Linter) run(req Request) (Response, error) {
 	for name, rl := range l.rules {
 		var config RuleConfig
 
-		if c, err := l.configs.getRuleConfig(req.ProtoFile().Path(), name); err == nil {
+		if c, err := l.configs.GetRuleConfig(req.ProtoFile().Path(), name); err == nil {
 			config = config.withOverride(c)
 		} else {
 			errMessages = append(errMessages, err.Error())
diff --git a/lint/source.go b/lint/source.go
index 7713011..ae361aa 100644
--- a/lint/source.go
+++ b/lint/source.go
@@ -157,6 +157,16 @@ func (s DescriptorSource) SyntaxComments() (Comments, error) {
 	return s.findCommentsByPath([]int{syntaxTag})
 }
 
+// PackageLocation returns the location of the package definition.
+func (s DescriptorSource) PackageLocation() (Location, error) {
+	return s.findLocationByPath([]int{packageTag})
+}
+
+// PackageComments returns the comments of the package definition.
+func (s DescriptorSource) PackageComments() (Comments, error) {
+	return s.findCommentsByPath([]int{packageTag})
+}
+
 // DescriptorLocation returns a `Location` for the given descriptor.
 // If not found, returns (nil, ErrPathNotFound).
 func (s DescriptorSource) DescriptorLocation(d protoreflect.Descriptor) (Location, error) {
@@ -173,6 +183,7 @@ func getPath(d protoreflect.Descriptor) []int {
 }
 
 const syntaxTag = 12
+const packageTag = 2
 
 var enumTagInFile = 5
 var enumTagInMessage = 4


Test patch:
diff --git a/lint/config_test.go b/lint/config_test.go
index deb7443..a3eb1c4 100644
--- a/lint/config_test.go
+++ b/lint/config_test.go
@@ -101,9 +101,9 @@ func TestRuleConfigs_getRuleConfig(t *testing.T) {
 		},
 	}
 	for ind, test := range tests {
-		cfg, _ := test.configs.getRuleConfig(test.path, test.rule)
+		cfg, _ := test.configs.GetRuleConfig(test.path, test.rule)
 		if cfg != test.result {
-			t.Errorf("Test #%d: %+v.getRuleConfig(%q, %q)=%+v; want %+v", ind+1, test.configs, test.path, test.rule, cfg, test.result)
+			t.Errorf("Test #%d: %+v.GetRuleConfig(%q, %q)=%+v; want %+v", ind+1, test.configs, test.path, test.rule, cfg, test.result)
 		}
 	}
 }
diff --git a/lint/source_test.go b/lint/source_test.go
index f557ec7..718b058 100644
--- a/lint/source_test.go
+++ b/lint/source_test.go
@@ -162,6 +162,35 @@ func TestSyntaxComments(t *testing.T) {
 	}
 }
 
+func TestPackageLocation(t *testing.T) {
+	req := readProtoFile(t, "test_source.protoset")
+	descSource := req.DescriptorSource()
+	want := Location{Position{5, 1}, Position{5, 25}}
+	got, err := descSource.PackageLocation()
+	if err != nil {
+		t.Errorf("PackageLocation() error: %s", err)
+	}
+	if !reflect.DeepEqual(got, want) {
+		t.Errorf("PackageLocation() returns %v, but want %v", got, want)
+	}
+}
+
+func TestPackageComments(t *testing.T) {
+	req := readProtoFile(t, "test_source.protoset")
+	descSource := req.DescriptorSource()
+
+	want := Comments{
+		TrailingComments: "package comment\n",
+	}
+	got, err := descSource.PackageComments()
+	if err != nil {
+		t.Errorf("PackageComments() error: %s", err)
+	}
+	if !reflect.DeepEqual(got, want) {
+		t.Errorf("PackageComments() returns %s, but want %s", got, want)
+	}
+}
+
 func TestIsRuleDisabled(t *testing.T) {
 	req := readProtoFile(t, "test_rule_disable.protoset")
 	fileDesc := req.ProtoFile()
diff --git a/lint/testdata/test_source.proto b/lint/testdata/test_source.proto
index 1d88335..524d021 100644
--- a/lint/testdata/test_source.proto
+++ b/lint/testdata/test_source.proto
@@ -2,7 +2,7 @@
 
 syntax = "proto2";
 
-package api.linter.lint;
+package api.linter.lint; //package comment
 
 // Outer
 message Outer {