github.com/blend/go-sdk@v1.20220411.3/testutil/golden_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package testutil_test
     9  
    10  import (
    11  	"flag"
    12  	"fmt"
    13  	"os"
    14  	"path/filepath"
    15  	"strings"
    16  	"testing"
    17  
    18  	"github.com/blend/go-sdk/assert"
    19  	"github.com/blend/go-sdk/testutil"
    20  	"github.com/blend/go-sdk/uuid"
    21  )
    22  
    23  const (
    24  	// NOTE: This is "peace of mind" by obscurity; we assume callers will
    25  	//       not use this flag, so we can register it with `MarkUpdateGoldenFlag()`
    26  	//       and then reference it in `TestAssertGoldenFile_Assert()`.
    27  	testUpdateGoldenFlag = "update-golden-0401b706-ae3c-4e7a-ba29-531fb811d3a2"
    28  )
    29  
    30  func TestGetTestFixture(t *testing.T) {
    31  	t.Parallel()
    32  	it := assert.New(t)
    33  
    34  	data := testutil.GetTestFixture(it, "sentinel.txt")
    35  	it.Equal("Any content will suffice here.\n", string(data))
    36  }
    37  
    38  func TestAssertGoldenFile_Assert(t *testing.T) {
    39  	t.Parallel()
    40  	it := assert.New(t)
    41  
    42  	expected := strings.Join([]string{
    43  		"#",
    44  		"# Copyright (c) 2021 - Present. Blend Labs, Inc. All rights reserved",
    45  		"# Blend Confidential - Restricted",
    46  		"#",
    47  		"a:",
    48  		"  b:",
    49  		"    c: 10",
    50  		"",
    51  	}, "\n")
    52  	testutil.AssertGoldenFile(it, []byte(expected), "config.yml", testutil.OptUpdateGoldenFlag(testUpdateGoldenFlag))
    53  }
    54  
    55  func TestAssertGoldenFile_Update(t *testing.T) {
    56  	t.Parallel()
    57  	it := assert.New(t)
    58  
    59  	// Generate a test fixtures directory we control.
    60  	tempDir, err := os.MkdirTemp("", "")
    61  	it.Nil(err)
    62  	t.Cleanup(func() {
    63  		os.RemoveAll(tempDir)
    64  	})
    65  	// Write a golden file to the test fixtures directory.
    66  	path := filepath.Join(tempDir, "golden.focus.txt")
    67  	err = os.WriteFile(path, []byte("short\n"), 0644)
    68  	it.Nil(err)
    69  
    70  	// NOTE: Add a per-run unique flag so that callers can't accidentally
    71  	//       force an update.
    72  	uniqueFlag := fmt.Sprintf("update-golden-%s", uuid.V4())
    73  
    74  	// Run `AssertGoldenFile` first in `assert` mode so the `goldenFileFlags`
    75  	// map can cache the flag (and then in subsequent runs we can enable
    76  	// the flag).
    77  	testutil.AssertGoldenFile(
    78  		it, []byte("short\n"), "focus.txt",
    79  		testutil.OptTestFixtureDirectory(tempDir),
    80  		testutil.OptUpdateGoldenFlag(uniqueFlag),
    81  	)
    82  
    83  	// Now forcefully turn the flag on.
    84  	f := flag.CommandLine.Lookup(uniqueFlag)
    85  	it.NotNil(f)
    86  	f.Value.Set("true")
    87  
    88  	// Run in `update` mode (not `assert`)
    89  	testutil.AssertGoldenFile(
    90  		it, []byte("long-extra\n"), "focus.txt",
    91  		testutil.OptTestFixtureDirectory(tempDir),
    92  		testutil.OptUpdateGoldenFlag(uniqueFlag),
    93  	)
    94  
    95  	// Finally verify the file was overwritten.
    96  	newContents, err := os.ReadFile(path)
    97  	it.Nil(err)
    98  	it.Equal([]byte("long-extra\n"), newContents)
    99  }