Stage 2 - Unit Test for Read a Blob Object

I passed this stage, but I’m trying to write a unit test for this stage. Here’s my function.

type NewReader func(r io.Reader) (io.ReadCloser, error)

func catFileCommand(fileSystem fs.FS, newReader NewReader, hash string) string {
	path := filepath.Join(hash[:2], hash[2:])
	f, _ := fileSystem.Open(path)
	r, _ := newReader(f)
	b, _ := io.ReadAll(r)
	parts := strings.Split(string(b), "\x00")
	return parts[1]

For the test I would like to create a function I could use in place of zlib.NewReader such that it no decompression is done. The raw string is just returned.

My test looks like this:

package main

import (

func TestCatFileCommand(t *testing.T) {

	fileSystem := fstest.MapFS{
		"e8/8f7a929cd70b0274c4ea33b209c97fa845fdbc": {Data: []byte("blob 11\x00hello world")},

	hash := "e88f7a929cd70b0274c4ea33b209c97fa845fdbc"
	got := catFileCommand(fileSystem, <pass-through>, hash)
	want := "hello world"

	if got != want {
		t.Errorf("got %s, want %s", got, want)

// TODO: need some type of pass through fake for zlib.NewReader to make this test work

Okay so I just ended up using zlib to compress the blog to a bytes.Buffer instead. Works fine.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.