github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/install/install_test.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  //go:build darwin
     5  // +build darwin
     6  
     7  package install
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/keybase/client/go/libkb"
    16  	"github.com/keybase/client/go/logger"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  var testLog = logger.New("test")
    21  
    22  func TestCommandLine(t *testing.T) {
    23  	testDir, err := os.MkdirTemp("", "kbbin")
    24  	defer os.RemoveAll(testDir)
    25  	if err != nil {
    26  		t.Fatalf("%s", err)
    27  	}
    28  
    29  	binPath, err := filepath.Abs(os.Args[0])
    30  	if err != nil {
    31  		t.Fatalf("%s", err)
    32  	}
    33  	linkPath := filepath.Join(testDir, "kbtest")
    34  
    35  	// Install
    36  	err = installCommandLineForBinPath(binPath, linkPath, true, testLog)
    37  	if err != nil {
    38  		t.Fatalf("%s", err)
    39  	}
    40  	_, err = os.Stat(linkPath)
    41  	if err != nil {
    42  		t.Fatalf("%s", err)
    43  	}
    44  
    45  	// Install again
    46  	err = installCommandLineForBinPath(binPath, linkPath, true, testLog)
    47  	if err != nil {
    48  		t.Fatalf("%s", err)
    49  	}
    50  	_, err = os.Stat(linkPath)
    51  	if err != nil {
    52  		t.Fatalf("%s", err)
    53  	}
    54  }
    55  
    56  func TestLastModifiedMatchingFile(t *testing.T) {
    57  	var err error
    58  	tc := libkb.SetupTest(t, "TestLastModifiedMatchingFile", 1)
    59  	defer tc.Cleanup()
    60  	tmpdir, err := os.MkdirTemp("", "TestLastModifiedMatchingFile")
    61  	defer os.RemoveAll(tmpdir)
    62  	require.NoError(t, err)
    63  	nameMatch := "blerg"
    64  	contentMatch := "lemon"
    65  	matchingContent := fmt.Sprintf("la la la\nblah blah\nblah%s a match!\n", contentMatch)
    66  	unmatchingContent := "la la la\nblah blah\nblah no matches\n"
    67  	filePattern := filepath.Join(tmpdir, fmt.Sprintf("*%s*", nameMatch))
    68  
    69  	// no matches with no files
    70  	match, err := LastModifiedMatchingFile(filePattern, contentMatch)
    71  	require.NoError(t, err)
    72  	require.Nil(t, match)
    73  
    74  	// no matches with two files that each only half match
    75  	err = os.WriteFile(filepath.Join(tmpdir, fmt.Sprintf("first%sfile.txt", nameMatch)), []byte(unmatchingContent), 0644)
    76  	require.NoError(t, err)
    77  	err = os.WriteFile(filepath.Join(tmpdir, "secondfile.txt"), []byte(matchingContent), 0644)
    78  	require.NoError(t, err)
    79  
    80  	match, err = LastModifiedMatchingFile(filePattern, contentMatch)
    81  	require.NoError(t, err)
    82  	require.Nil(t, match)
    83  
    84  	// with an actual match
    85  	fullPath := filepath.Join(tmpdir, fmt.Sprintf("third%sfile.txt", nameMatch))
    86  	err = os.WriteFile(fullPath, []byte(matchingContent), 0644)
    87  	require.NoError(t, err)
    88  	match, err = LastModifiedMatchingFile(filePattern, contentMatch)
    89  	require.NoError(t, err)
    90  	require.NotNil(t, match)
    91  	require.Equal(t, fullPath, *match)
    92  
    93  	// with another match
    94  	fullPath = filepath.Join(tmpdir, fmt.Sprintf("fourth%sfile.txt", nameMatch))
    95  	err = os.WriteFile(fullPath, []byte(matchingContent), 0644)
    96  	require.NoError(t, err)
    97  	match, err = LastModifiedMatchingFile(filePattern, contentMatch)
    98  	require.NoError(t, err)
    99  	require.NotNil(t, match)
   100  	require.Equal(t, fullPath, *match)
   101  
   102  	// result doesn't change after additional files are added
   103  	err = os.WriteFile(filepath.Join(tmpdir, fmt.Sprintf("fifth%sfile.txt", nameMatch)), []byte(unmatchingContent), 0644)
   104  	require.NoError(t, err)
   105  	err = os.WriteFile(filepath.Join(tmpdir, "sixthfile.txt"), []byte(matchingContent), 0644)
   106  	require.NoError(t, err)
   107  	match, err = LastModifiedMatchingFile(filePattern, contentMatch)
   108  	require.NoError(t, err)
   109  	require.NotNil(t, match)
   110  	require.Equal(t, fullPath, *match)
   111  }