github.com/SpiffyEight77/magefiles@v0.6.8/releases/publish_test.go (about)

     1  package releases
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/hex"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"get.porter.sh/magefiles/porter"
    11  	"github.com/carolynvs/magex/mgx"
    12  	"github.com/carolynvs/magex/shx"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestGetReleaseAssets(t *testing.T) {
    18  	tmp, err := os.MkdirTemp("", "magefiles")
    19  	require.NoError(t, err)
    20  	defer os.RemoveAll(tmp)
    21  
    22  	mgx.Must(shx.Copy("testdata/mixins/v1.2.3/*", tmp, shx.CopyRecursive))
    23  
    24  	gotFiles, err := getReleaseAssets(tmp)
    25  	require.NoError(t, err)
    26  
    27  	wantFiles := []string{
    28  		filepath.Join(tmp, "mymixin-darwin-amd64"),
    29  		filepath.Join(tmp, "mymixin-darwin-amd64.sha256sum"),
    30  		filepath.Join(tmp, "mymixin-linux-amd64"),
    31  		filepath.Join(tmp, "mymixin-linux-amd64.sha256sum"),
    32  		filepath.Join(tmp, "mymixin-windows-amd64.exe"),
    33  		filepath.Join(tmp, "mymixin-windows-amd64.exe.sha256sum"),
    34  	}
    35  	assert.Equal(t, wantFiles, gotFiles)
    36  
    37  	// Read the existing checksum file with stale contents, and ensure it was updated
    38  	gotChecksum, err := os.ReadFile(filepath.Join(tmp, "mymixin-darwin-amd64.sha256sum"))
    39  	require.NoError(t, err)
    40  	wantCheckSum := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  mymixin-darwin-amd64"
    41  	assert.Equal(t, wantCheckSum, string(gotChecksum))
    42  }
    43  
    44  func TestAddChecksumExt(t *testing.T) {
    45  	tests := []struct {
    46  		input         string
    47  		expectedAdded bool
    48  		expected      string
    49  	}{
    50  		{
    51  			input:         "porter.sh",
    52  			expectedAdded: true,
    53  			expected:      "porter.sh.sha256sum",
    54  		},
    55  		{
    56  			input:         "porter.sh.sha256sum",
    57  			expectedAdded: false,
    58  			expected:      "porter.sh.sha256sum",
    59  		},
    60  	}
    61  
    62  	for _, tt := range tests {
    63  		tt := tt
    64  		t.Run("", func(t *testing.T) {
    65  			output, added := AddChecksumExt(tt.input)
    66  			assert.Equal(t, tt.expected, output)
    67  			assert.Equal(t, tt.expectedAdded, added)
    68  		})
    69  	}
    70  
    71  }
    72  
    73  func TestAppendDataPath(t *testing.T) {
    74  	data := make([]byte, 10)
    75  	_, err := rand.Read(data)
    76  	require.NoError(t, err)
    77  	dataPath := "test/random"
    78  	expected := hex.EncodeToString(data) + "  random"
    79  
    80  	output := AppendDataPath(data, dataPath)
    81  	require.Equal(t, expected, output)
    82  }
    83  
    84  func TestGenerateMixinFeed(t *testing.T) {
    85  	tmp, err := os.MkdirTemp("", "magefiles")
    86  	require.NoError(t, err)
    87  	defer os.RemoveAll(tmp)
    88  
    89  	// Install porter in our test bin
    90  	tmpBin := filepath.Join(tmp, "bin")
    91  	require.NoError(t, shx.Copy("../bin", tmpBin, shx.CopyRecursive), "failed to copy the porter bin into the test directory")
    92  	porter.UsePorterHome(tmpBin)
    93  
    94  	// Copy our atom feed template
    95  	buildDir := filepath.Join(tmp, "build")
    96  	require.NoError(t, os.Mkdir(buildDir, 0770))
    97  	require.NoError(t, shx.Copy("testdata/atom-template.xml", buildDir))
    98  
    99  	// Make a fake mixin release
   100  	require.NoError(t, shx.Copy("testdata/mixins", tmpBin, shx.CopyRecursive))
   101  
   102  	// Change into the tmp directory since the publish logic uses relative file paths
   103  	origDir, err := os.Getwd()
   104  	require.NoError(t, err)
   105  	require.NoError(t, os.Chdir(tmp))
   106  	defer func() {
   107  		require.NoError(t, os.Chdir(origDir))
   108  	}()
   109  
   110  	err = GenerateMixinFeed()
   111  	require.NoError(t, err)
   112  
   113  	assert.FileExists(t, filepath.Join(tmpBin, "mixins/.packages/mixins/atom.xml"), "expected a mixin feed")
   114  }
   115  
   116  func TestGeneratePluginFeed_PorterNotInstalled(t *testing.T) {
   117  	tmp, err := os.MkdirTemp("", "magefiles")
   118  	require.NoError(t, err)
   119  	defer os.RemoveAll(tmp)
   120  
   121  	// DO NOT INSTALL PORTER INTO THE BIN
   122  	tmpBin := filepath.Join(tmp, "bin")
   123  
   124  	// Copy our atom feed template
   125  	buildDir := filepath.Join(tmp, "build")
   126  	require.NoError(t, os.Mkdir(buildDir, 0770))
   127  	require.NoError(t, shx.Copy("testdata/atom-template.xml", buildDir))
   128  
   129  	// Make a fake mixin release
   130  	require.NoError(t, shx.Copy("testdata/mixins", tmpBin, shx.CopyRecursive))
   131  
   132  	// Change into the tmp directory since the publish logic uses relative file paths
   133  	origDir, err := os.Getwd()
   134  	require.NoError(t, err)
   135  	require.NoError(t, os.Chdir(tmp))
   136  	defer func() {
   137  		require.NoError(t, os.Chdir(origDir))
   138  	}()
   139  
   140  	err = GeneratePluginFeed()
   141  	require.Errorf(t, err, "farts", "GeneratePluginFeed should fail when porter is not in the bin")
   142  }