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

[util][testutil] Add dump val as pretty json #92

Closed
at15 opened this issue Sep 17, 2018 · 1 comment
Closed

[util][testutil] Add dump val as pretty json #92

at15 opened this issue Sep 17, 2018 · 1 comment
Labels
Milestone

Comments

@at15
Copy link
Member

at15 commented Sep 17, 2018

https://github.com/dyweb/gommon/blob/master/util/testutil/data.go only contains read but no write

Currently we only have

func WriteFixture(t *testing.T, path string, data []byte) {
	err := ioutil.WriteFile(path, data, 0664)
	if err != nil {
		t.Fatalf("can't write fixture %s: %v", path, err)
	}
}

Need

func SaveAsJson(t *testing.T, v interface{}, file string) {
	b, err := json.Marshal(v)
	if err != nil {
		t.Fatalf("failed to encode as json %v", v)
		return
	}
	if err := ioutil.WriteFile(file, b, 0664); err != nil {
		t.Fatalf("failed to save file %s: %v", file, err)
		return
	}
}

func SaveAsPrettyJson(t *testing.T, v interface{}, file string) {
	b, err := json.MarshalIndent(v, "", "  ")
	if err != nil {
		t.Fatalf("failed to encode as json %v", v)
		return
	}
	if err := ioutil.WriteFile(file, b, 0664); err != nil {
		t.Fatalf("failed to save file %s: %v", file, err)
		return
	}
	t.Logf("saved json to %s", file)
}

func SaveAsPrettyJsonf(t *testing.T, v interface{}, format string, args ...interface{}) {
	file := fmt.Sprintf(format, args...)
	b, err := json.MarshalIndent(v, "", "  ")
	if err != nil {
		t.Fatalf("failed to encode as json %v", v)
		return
	}
	if err := ioutil.WriteFile(file, b, 0664); err != nil {
		t.Fatalf("failed to save file %s: %v", file, err)
		return
	}
	t.Logf("saved json to %s", file)
}
@at15 at15 added the pkg/util label Sep 17, 2018
at15 added a commit that referenced this issue Sep 22, 2018
- disable server handler check in unix server listen test, it failed CI
because when close the server/remove socket file it seems the server
didn't shut down gracefully ... https://travis-ci.org/dyweb/gommon/jobs/431772857
@at15 at15 added this to the 0.0.8 milestone Sep 22, 2018
@at15
Copy link
Member Author

at15 commented Nov 26, 2018

already in #90

@at15 at15 closed this as completed Nov 26, 2018
at15 added a commit that referenced this issue Nov 26, 2018
- actually they are alredy there in pretty.go under same package ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant