Skip to content

Add test of json.go #13106

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

Merged
merged 3 commits into from
Jul 27, 2025
Merged

Add test of json.go #13106

merged 3 commits into from
Jul 27, 2025

Conversation

suwakei
Copy link
Contributor

@suwakei suwakei commented Jul 27, 2025

What I did

Missing test:
There is no corresponding test (json_test.go) in this file. We have added a unit test to ensure that jsonWriter produces JSON as expected.
This change will greatly improve code reliability and maintainability.

Fix:

Add a new test file json_test.go to verify the behavior of jsonWriter.

compose/pkg/progress/json_test.go

/*
   Copyright 2024 Docker Compose CLI authors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package progress

import (
	"bytes"
	"context"
	"encoding/json"
	"testing"

	"gotest.tools/v3/assert"
)

func TestJsonWriter_Event(t *testing.T) {
	var out bytes.Buffer
	w := &jsonWriter{
		out:    &out,
		done:   make(chan bool),
		dryRun: true,
	}

	event := Event{
		ID:         "service1",
		ParentID:   "project",
		Text:       "Creating",
		StatusText: "Working",
		Current:    50,
		Total:      100,
		Percent:    50,
	}
	w.Event(event)

	var msg jsonMessage
	err := json.Unmarshal(out.Bytes(), &msg)
	assert.NilError(t, err)

	assert.Equal(t, true, msg.DryRun)
	assert.Equal(t, false, msg.Tail)
	assert.Equal(t, "service1", msg.ID)
	assert.Equal(t, "project", msg.ParentID)
	assert.Equal(t, "Creating", msg.Text)
	assert.Equal(t, "Working", msg.Status)
	assert.Equal(t, int64(50), msg.Current)
	assert.Equal(t, int64(100), msg.Total)
	assert.Equal(t, 50, msg.Percent)
}

func TestJsonWriter_TailMsgf(t *testing.T) {
	var out bytes.Buffer
	w := &jsonWriter{
		out:    &out,
		done:   make(chan bool),
		dryRun: false,
	}

	go func() {
		_ = w.Start(context.Background())
	}()

	w.TailMsgf("hello %s", "world")

	w.Stop()

	var msg jsonMessage
	err := json.Unmarshal(out.Bytes(), &msg)
	assert.NilError(t, err)

	assert.Equal(t, false, msg.DryRun)
	assert.Equal(t, true, msg.Tail)
	assert.Equal(t, "hello world", msg.Text)
	assert.Equal(t, "", msg.ID)
	assert.Equal(t, "", msg.Status)
}

Signed-off-by: keitosuwahara <[email protected]>
@suwakei suwakei requested a review from a team as a code owner July 27, 2025 08:13
@suwakei suwakei requested review from ndeloof and glours July 27, 2025 08:13
err := json.Unmarshal(out.Bytes(), &msg)
assert.NilError(t, err)

assert.Equal(t, true, msg.DryRun)
Copy link
Contributor

Choose a reason for hiding this comment

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

you can use DeepEqual to compare expected vs actual struct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review and suggestion! I've updated the test to use DeepEqual.

Copy link

codecov bot commented Jul 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Signed-off-by: keitosuwahara <[email protected]>
@ndeloof ndeloof enabled auto-merge (rebase) July 27, 2025 12:46
Signed-off-by: keitosuwahara <[email protected]>
auto-merge was automatically disabled July 27, 2025 12:55

Head branch was pushed to by a user without write access

@ndeloof ndeloof enabled auto-merge (rebase) July 27, 2025 12:59
@ndeloof ndeloof merged commit 1946de5 into docker:main Jul 27, 2025
23 of 26 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Aug 11, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [docker/compose](https://github.com/docker/compose) | patch | `v2.39.1` -> `v2.39.2` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>docker/compose (docker/compose)</summary>

### [`v2.39.2`](https://github.com/docker/compose/releases/tag/v2.39.2)

[Compare Source](docker/compose@v2.39.1...v2.39.2)

#### What's Changed

##### 🐛 Fixes

- Fix (regression): compose build render build output with tty support by [@&#8203;ndeloof](https://github.com/ndeloof) in docker/compose#13107
- Add missing `_MODEL` suffix to model variable pass to dependent services of a model by [@&#8203;glours](https://github.com/glours) in docker/compose#13109
- Apply `BUILDKIT_PROGRESS` value when building with bake by [@&#8203;glours](https://github.com/glours) in docker/compose#13110
- Define `pull` and `no_cache` from either service or flags values when building with bake by [@&#8203;glours](https://github.com/glours) in docker/compose#13133
- Only monitor attached services on `up` command by [@&#8203;glours](https://github.com/glours) in docker/compose#13114

##### 🔧  Internal

- Add Streams Comment by [@&#8203;suwakei](https://github.com/suwakei) in docker/compose#13103
- Add test of `json.go` by [@&#8203;suwakei](https://github.com/suwakei) in docker/compose#13106
- Refactoring of redundant condition checks by [@&#8203;suwakei](https://github.com/suwakei) in docker/compose#13104
- Eliminated magic string by [@&#8203;suwakei](https://github.com/suwakei) in docker/compose#13105
- Use log API for containers we didn't attached to by [@&#8203;ndeloof](https://github.com/ndeloof) in docker/compose#13111
- Use `cli-plugins/metadata` package by [@&#8203;thaJeztah](https://github.com/thaJeztah) in docker/compose#13130
- `pkg/compose`: simplify getting auth-config key by [@&#8203;thaJeztah](https://github.com/thaJeztah) in docker/compose#13120
- Add go as a prerequisite in build instructions by [@&#8203;mattrunyon](https://github.com/mattrunyon) in docker/compose#13131

##### ⚙️ Dependencies

- Build(deps): bump github.com/docker/cli from `28.3.2+incompatible` to `28.3.3+incompatible` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in docker/compose#13116
- Build(deps): bump github.com/docker/docker from `28.3.2+incompatible` to `28.3.3+incompatible` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in docker/compose#13115
- Build(deps): bump github.com/containerd/containerd/v2 from `2.1.3` to `2.1.4` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in docker/compose#13119
- Build(deps): bump github.com/docker/go-connections from `0.5.0` to `0.6.0` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in docker/compose#13137
- Build(deps): bump golang.org/x/sys from `0.34.0` to `0.35.0` by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in docker/compose#13138
- Bump golang to `1.23.12` by [@&#8203;austinvazquez](https://github.com/austinvazquez) in docker/compose#13142

#### New Contributors

- [@&#8203;mattrunyon](https://github.com/mattrunyon) made their first contribution in docker/compose#13131
- [@&#8203;austinvazquez](https://github.com/austinvazquez) made their first contribution in docker/compose#13142

**Full Changelog**: docker/compose@v2.39.1...v2.39.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS41OC4yIiwidXBkYXRlZEluVmVyIjoiNDEuNTguMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants