Skip to content

client: replace difflib with go-cmp in compatibility tests - #7054

Draft
thaJeztah wants to merge 1 commit into
moby:masterfrom
thaJeztah:difflib
Draft

client: replace difflib with go-cmp in compatibility tests#7054
thaJeztah wants to merge 1 commit into
moby:masterfrom
thaJeztah:difflib

Conversation

@thaJeztah

Copy link
Copy Markdown
Member

Just a quick draw-up; did a basic example what it could look like;

package main

import (
	"encoding/json"
	"fmt"

	"github.com/google/go-cmp/cmp"
)

func main() {
	expected := []byte(`{
		"architecture": "amd64",
		"config": {
			"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],
			"Cmd": ["sh"],
			"WorkingDir": ""
		},
		"created": "2015-10-21T07:28:00Z",
		"os": "linux",
		"rootfs": {
			"type": "layers",
			"diff_ids": [
				"sha256:fe4922f0a3ca2aa707ca0f8cc53937e947fec1157a09302baf11904258d67d17",
				"sha256:007165e99d6304af5653e8a8cc1c8886e5719cf31bf09cbd9f50fa09008ef654"
			]
		}
	}`)

	actual := []byte(`{
		"architecture": "amd64",
		"config": {
			"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],
			"Cmd": ["bash"],
			"WorkingDir": "/work"
		},
		"created": "2015-10-21T07:28:00Z",
		"os": "linux",
		"rootfs": {
			"type": "layers",
			"diff_ids": [
				"sha256:fe4922f0a3ca2aa707ca0f8cc53937e947fec1157a09302baf11904258d67d17",
				"sha256:deadbeef"
			]
		}
	}`)

	fmt.Println(cmp.Diff(normalizeJSON(expected), normalizeJSON(actual)))
}

func normalizeJSON(dt []byte) string {
	var v any
	if err := json.Unmarshal(dt, &v); err != nil {
		return string(dt)
	}
	dt, err := json.MarshalIndent(v, "", "  ")
	if err != nil {
		return string(dt)
	}
	return string(dt)
}

Outputs;

(
  	"""
  	... // 2 identical lines
  	  "config": {
  	    "Cmd": [
- 	      "sh"
+ 	      "bash"
  	    ],
  	    "Env": [
  	      "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  	    ],
- 	    "WorkingDir": ""
+ 	    "WorkingDir": "/work"
  	  },
  	  "created": "2015-10-21T07:28:00Z",
  	... // 2 identical lines
  	    "diff_ids": [
  	      "sha256:fe4922f0a3ca2aa707ca0f8cc53937e947fec1157a09302baf11904258d67d17",
- 	      "sha256:007165e99d6304af5653e8a8cc1c8886e5719cf31bf09cbd9f50fa09008ef654"
+ 	      "sha256:deadbeef"
  	    ],
  	    "type": "layers"
  	... // 2 identical lines
  	"""
  )

@github-actions github-actions Bot added area/testing area/dependencies Pull requests that update a dependency file area/client labels Aug 19, 2026
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/client area/dependencies Pull requests that update a dependency file area/storage area/testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant