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

refactor input functions to make them testable #255

Closed
wants to merge 8 commits into from
Closed

Conversation

taimoorgit
Copy link
Contributor

@taimoorgit taimoorgit commented Mar 5, 2024

Issues

https://github.com/OpsLevel/team-platform/issues/264

Changelog

  • Divide readResourceInput into 3 functions: one to read input, one to apply input, one to do both
  • Change input function signatures to allow injecting test data by using variadic arguments
  • Add tests for input functions

@taimoorgit taimoorgit added the enhancement New feature or request label Mar 5, 2024
@taimoorgit taimoorgit self-assigned this Mar 5, 2024
@taimoorgit taimoorgit force-pushed the ta/sp-mar-5 branch 2 times, most recently from 62d4064 to 8cd5e95 Compare March 5, 2024 20:37
@taimoorgit taimoorgit changed the title add support for PD allowed in config files and prop locked add support for PropertyDisplayStatus and Locked to CLI Mar 5, 2024
@taimoorgit taimoorgit changed the title add support for PropertyDisplayStatus and Locked to CLI refactor input functions and add tests Mar 5, 2024
@taimoorgit taimoorgit force-pushed the ta/sp-mar-5 branch 2 times, most recently from c67a24c to 70d0827 Compare March 6, 2024 00:09
src/cmd/input.go Outdated
return nil
}

func ReadResourceHandleJSONFields[T any](input []byte) (*T, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like this is ONLY used in test files - this shouldn't be part of the codebase then it should be a utility function in the testing code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same with its helper functions handleJSONString and handleJSONSchema

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rocktavious this is used in the properties PR that follows this: #257

Copy link
Collaborator

Choose a reason for hiding this comment

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

In fact after reading the whole code - i don't see where this function is used - so why are we testing it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rocktavious I wanted to add an abstraction in case we would start adding more resource types that use JSON fields outside of properties, but I have now reversed this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like the abstraction is more complex and unexpected then having custom yaml marshallars

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rocktavious if we remove support for JSON, we could replace this code with only custom YAML marshalers which would be much cleaner. Supporting both YAML and JSON feels hacky.

Also, while you can read in JSON objects, reading in JSON values (number, string, boolean, list) is next to impossible without us writing another 100+ lines of code.

How do you feel about only supporting YAML?

Copy link
Collaborator

@rocktavious rocktavious Mar 6, 2024

Choose a reason for hiding this comment

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

#255 (comment)

I think i already voiced my approval of having the yaml way only

src/cmd/input.go Outdated
return &finalInput, nil
}

func ReadResourceInput[T any](input []byte) (*T, error) {
Copy link
Collaborator

@rocktavious rocktavious Mar 6, 2024

Choose a reason for hiding this comment

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

The only reason input is here is because of the tests - we should refactor this so it doesn't take input (because the only thing i should be doing is "reading input"

The function that takes an input argument should be a helper function in the test code.

this will also remove the need to add all the (nil) to the codebased where this function is used.

In the future it's worth remembering to not conflate test code with production code. Keeping separated concerns and making a function do 1 logical thing will help.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rocktavious being able to pass in input can serve a purpose outside of tests as well, for example by being to do readInput() to get input in bytes that can be used to get the raw input from the user before calling ReadResourceInput().

Copy link
Collaborator

Choose a reason for hiding this comment

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

This add complexity for a "what if" that we are currently not doing in our code outside of testing nor would likely do IMO.

Is there any reason that the tests cannot just use ReadResource[T](input) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rocktavious I have changed the ReadResourceInput signature to use a variadic argument for the mocked inputs. Is that OK? It means that we don't need to (nil) every time.

Copy link
Collaborator

@rocktavious rocktavious Mar 6, 2024

Choose a reason for hiding this comment

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

thats definitely better - i still don't like it because now you are abusing variadic - especially because you don't handle multiple arguments so if someone passes 2 things they get unexpected behaviour of reading from standard input or from a file - but we can move on since this gets what i'm after it just leaves tech debt in our codebase sadly.

src/cmd/input.go Outdated Show resolved Hide resolved
@taimoorgit taimoorgit added bug Something isn't working and removed enhancement New feature or request labels Mar 6, 2024
@taimoorgit taimoorgit changed the title refactor input functions and add tests refactor input functions to make them testable Mar 6, 2024
@taimoorgit taimoorgit marked this pull request as draft March 7, 2024 02:45
@taimoorgit taimoorgit removed the request for review from davidbloss March 21, 2024 16:55
@taimoorgit taimoorgit closed this Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants