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

convert sync tests to go #1389

Merged
merged 24 commits into from May 11, 2020
Merged

convert sync tests to go #1389

merged 24 commits into from May 11, 2020

Conversation

charlierudolph
Copy link
Collaborator

Opening for visibility, not ready for review

@charlierudolph charlierudolph requested a review from kevgo May 8, 2020 05:51
@charlierudolph charlierudolph changed the title WIP: convert sync tests to go convert sync tests to go May 10, 2020
# Conflicts:
#	Makefile
#	test/git_environment.go
#	test/steps/configuration_steps.go
#	test/steps/merge_steps.go
@kevgo
Copy link
Contributor

kevgo commented May 11, 2020

In case it helps, since there are so many sync tests, it might be more manageable to do one pull request per subdirectory.

@charlierudolph charlierudolph merged commit c133086 into master May 11, 2020
@charlierudolph charlierudolph deleted the cr-sync branch May 11, 2020 16:15
Copy link
Contributor

@kevgo kevgo left a comment

Choose a reason for hiding this comment

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

@charlierudolph I really don't know how you always do this and am in awe. You have converted at least as many specs as I have over the last months, but you did in a couple of days. Some suggestions below, curious what you think.

@@ -61,6 +61,13 @@ func (builder *CommitTableBuilder) Add(commit Commit, location string) {
}
}

// AddMany registers the given commits from the given location into this table.
func (builder *CommitTableBuilder) AddMany(commits []Commit, location string) {
for _, commit := range commits {
Copy link
Contributor

Choose a reason for hiding this comment

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

This type of loop creates a copy of the current commit in each loop iteration. Which is fine for small values like when iterating lists of int. Commits are, however, pretty large objects that consist of several strings. Here is a version that uses the commits in the list, i.e. avoids making a copy:

for c := range commits {
  builder.Add(commits[c], location)
}

Probably not particularly relevant in this context, just wanted to share.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes sense. Hmm, would using an array of commit pointers be equivalent in efficiency? If so, I think I'd prefer we switch to that

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer to not switch to pointers for everything. They have other side effects like additional indirection on access (=slowness), and possibly more work for the garbage collector. Plus, on 64 bit systems pointers require an additional 8 bytes for every entry.

I think it's better to use the loop it was designed in Go: with the loop index as the primary element, and using the optional copy of the current item only when that makes sense.

err = result.DeveloperRepo.Fetch()
if err != nil {
return &result, fmt.Errorf("cannot fetch: %w", err)
for _, repo := range []GitRepository{result.DeveloperRepo, result.CoworkerRepo} {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since coworker repos are extremely rarely used, what do you think about creating them only when needed? This could be done via an AddCoworker method of GitEnvironment, similar to AddUpstream.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good

func (env GitEnvironment) CreateTags(table *messages.PickleStepArgument_PickleTable) error {
columnNames := helpers.TableFields(table)
if columnNames[0] != "NAME" && columnNames[1] != "LOCATION" {
return fmt.Errorf("tag table must have columns NAME and LOCATION")
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice that you do this check and provide a helpful error message!

@@ -279,6 +298,15 @@ func (repo *GitRepository) CurrentBranch() (result string, err error) {
return strings.TrimSpace(outcome.OutputSanitized()), nil
}

// CurrentFileContent provides the current content of a file.
func (repo *GitRepository) CurrentFileContent(filename string) (result string, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts about naming this FileContent? We already have FileContentInCommit(she, filename), FilesInCommit(sha), and FilesInBranch. I think a FileContent method fits naturally in here, and the name makes clear that this is the content of the given file in the given repo.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay that works

if err != nil {
return result, fmt.Errorf("cannot determine files in branch %q in repo %q: %w", branch, repo.Dir, err)
}
for _, line := range strings.Split(strings.TrimSpace(outcome.OutputSanitized()), "\n") {
Copy link
Contributor

@kevgo kevgo May 11, 2020

Choose a reason for hiding this comment

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

FYI Result.OutputSanitized already runs strings.TrimSpace on the output. Since this is wrong in a lot of my code as well (and you probably followed it as a reference here), I am cleaning this up in #1403, so nothing to do here except to accept my apologies.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the cleanup!


// TagSteps defines Gherkin step implementations around merges.
func TagSteps(suite *godog.Suite, fs *FeatureState) {
suite.Step(`^I have the following tags$`, func(table *messages.PickleStepArgument_PickleTable) error {
Copy link
Contributor

@kevgo kevgo May 11, 2020

Choose a reason for hiding this comment

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

People don't have tags 🙂 Thoughts about renaming this to my repo has the following tags? Also okay to do efficiently as a search-and-replace once the conversion is done.

@@ -0,0 +1,61 @@
package test
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo in filename tag_table_bulider.go

result := TagTableBuilder{
tagToLocations: make(map[string]helpers.OrderedStringSet),
}
return result
Copy link
Contributor

Choose a reason for hiding this comment

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

You could inline the result variable.


// AddMany registers the given tags from the given location into this table.
func (builder *TagTableBuilder) AddMany(tags []string, location string) {
for _, tag := range tags {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider more efficient loop here.

// Table provides the data accumulated by this TagTableBuilder as a DataTable.
func (builder *TagTableBuilder) Table() (result DataTable) {
result.AddRow("NAME", "LOCATION")
tags := make([]string, len(builder.tagToLocations))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice preallocation at the correct length!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Linter made me do it haha

This was referenced May 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants