Skip to content

Commit

Permalink
add test for ensuring global variable is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
austinvalle committed May 3, 2024
1 parent a58e305 commit e089b65
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
50 changes: 50 additions & 0 deletions tfprotov5/tf5server/proto_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package tf5server

import (
"bufio"
"os"
"regexp"
"testing"
)

// MAINTAINER NOTE: This test is a best effort for ensuring that the protocol version variables in the tf5server package
// stay in sync with the actual protocol file.
func Test_EnsureVersionConstantMatchesProtoFile(t *testing.T) {
t.Parallel()

file, err := os.Open("../internal/tfplugin5/tfplugin5.proto")
if err != nil {
t.Fatalf("error opening proto file: %s", err)
}
defer file.Close()

protoFileComment := regexp.MustCompile(`(?:Terraform Plugin RPC protocol version )(\d+.\d+)+`)

var expectedProtocolVersion string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
matches := protoFileComment.FindStringSubmatch(line)

if len(matches) > 1 {
expectedProtocolVersion = matches[1]
break
}
}

if err := scanner.Err(); err != nil {
t.Fatalf("error scanning proto file: %s", err)
}

if expectedProtocolVersion == "" {
t.Fatalf("couldn't find version comment in proto file: %s", err)
}

if protocolVersion != expectedProtocolVersion {
t.Errorf("protocol version Go variable is different from proto file - expected: %s, got: %s\n", expectedProtocolVersion, protocolVersion)
t.Log("MAINTAINER NOTE: Update tf5server.protocolVersionMajor and tf5server.protocolVersionMinor to match the proto file.")
}
}
2 changes: 1 addition & 1 deletion tfprotov5/tf5server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
//
// In the future, it may be possible to include this information directly
// in the protocol buffers rather than recreating a constant here.
protocolVersionMinor uint = 4
protocolVersionMinor uint = 6
)

// protocolVersion represents the combined major and minor version numbers of
Expand Down
50 changes: 50 additions & 0 deletions tfprotov6/tf6server/proto_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package tf6server

import (
"bufio"
"os"
"regexp"
"testing"
)

// MAINTAINER NOTE: This test is a best effort for ensuring that the protocol version variables in the tf6server package
// stay in sync with the actual protocol file.
func Test_EnsureVersionConstantMatchesProtoFile(t *testing.T) {
t.Parallel()

file, err := os.Open("../internal/tfplugin6/tfplugin6.proto")
if err != nil {
t.Fatalf("error opening proto file: %s", err)
}
defer file.Close()

protoFileComment := regexp.MustCompile(`(?:Terraform Plugin RPC protocol version )(\d+.\d+)+`)

var expectedProtocolVersion string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
matches := protoFileComment.FindStringSubmatch(line)

if len(matches) > 1 {
expectedProtocolVersion = matches[1]
break
}
}

if err := scanner.Err(); err != nil {
t.Fatalf("error scanning proto file: %s", err)
}

if expectedProtocolVersion == "" {
t.Fatalf("couldn't find version comment in proto file: %s", err)
}

if protocolVersion != expectedProtocolVersion {
t.Errorf("protocol version Go variable is different from proto file - expected: %s, got: %s", expectedProtocolVersion, protocolVersion)
t.Log("MAINTAINER NOTE: Update tf6server.protocolVersionMajor and tf6server.protocolVersionMinor to match the proto file.")
}
}
2 changes: 1 addition & 1 deletion tfprotov6/tf6server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
//
// In the future, it may be possible to include this information directly
// in the protocol buffers rather than recreating a constant here.
protocolVersionMinor uint = 4
protocolVersionMinor uint = 6
)

// protocolVersion represents the combined major and minor version numbers of
Expand Down

0 comments on commit e089b65

Please sign in to comment.