Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra TestCheckFuncs to check attribute's string length #893

Merged
merged 7 commits into from
Mar 2, 2022

Conversation

detro
Copy link
Contributor

@detro detro commented Feb 23, 2022

Background

Sometimes, when writing tests for a provider, one needs to check that a value is a certain length or length range, while cannot rely on expecting a specific value to match. This is for example true for situations when the provider will generate a value entirely randomised, but that "should be at least this long/short".

Solution proposed

2 additional functions in helper/resource/testing.go:

  • func TestRangeLengthResourceAttr(name, key string, min, max int) TestCheckFunc
  • func TestMatchLengthResourceAttr(name, key string, length int) TestCheckFunc

This PR comes with tests to exercise this utility.

Ivan De Marino added 2 commits February 23, 2022 18:55
… to check resource attribute string length
This is based on the official [GitHub templates](https://github.com/github/gitignore), plus something for Jetbrains products
@detro detro added enhancement New feature or request proposal labels Feb 23, 2022
@detro detro requested a review from a team as a code owner February 23, 2022 19:07
@bookshelfdave
Copy link
Contributor

I'll suggest moving the .gitignore changes to a separate PR.

helper/resource/testing.go Outdated Show resolved Hide resolved
@@ -1031,6 +1031,36 @@ func testCheckResourceAttrPair(isFirst *terraform.InstanceState, nameFirst strin
return nil
}

// TestRangeLengthResourceAttr is a TestCheckFunc which checks that the length of the value
// in state for the given name/key is within an expected (closed) range
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it matters in this repo, but the last linter PR I looked at wanted .'s at the end of comments.

helper/resource/testing_test.go Show resolved Hide resolved
helper/resource/testing.go Outdated Show resolved Hide resolved
@detro detro requested a review from bookshelfdave March 1, 2022 18:54
helper/resource/testing.go Outdated Show resolved Hide resolved
@detro detro merged commit fe75bb1 into main Mar 2, 2022
@detro detro deleted the detro/extra_TestCheckFunc_for_attribute_string_length branch March 2, 2022 13:14
detro added a commit that referenced this pull request Mar 8, 2022
detro pushed a commit that referenced this pull request Mar 8, 2022
@github-actions
Copy link

github-actions bot commented Apr 2, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request proposal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants