Skip to content

Commit

Permalink
Fixed branch ID validation (#55)
Browse files Browse the repository at this point in the history
Fixed branch ID validation
  • Loading branch information
kislerdm authored Nov 2, 2023
2 parents 1da7547 + 66dd18b commit 1a0341d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.2.5] - 2023-11-02

### Fixed

- Fixed validation of the [branch ID](https://neon.tech/docs/manage/branches#view-branches).

## [v0.2.4] - 2023-10-26

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/resource_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,6 @@ func resourceBranchImport(ctx context.Context, d *schema.ResourceData, meta inte
}

func isValidBranchID(s string) bool {
return strings.HasPrefix("br-", s)
const prefix = "br-"
return strings.HasPrefix(s, prefix) && len(strings.TrimPrefix(s, prefix)) > 0
}
50 changes: 50 additions & 0 deletions internal/provider/resource_branch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package provider

import "testing"

func Test_isValidBranchID(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "happy path",
args: args{
s: "br-foo123b",
},
want: true,
},
{
name: "unhappy path: no id post-prefix",
args: args{
s: "br-",
},
want: false,
},
{
name: "unhappy path: no prefix",
args: args{
s: "qux",
},
want: false,
},
{
name: "unhappy path: empty string",
args: args{
s: "",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isValidBranchID(tt.args.s); got != tt.want {
t.Errorf("isValidBranchID() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1a0341d

Please sign in to comment.