github.com/goreleaser/goreleaser@v1.25.1/internal/gio/chtimes_test.go (about)

     1  package gio
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strconv"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestChtimes(t *testing.T) {
    15  	modTime := time.Now().AddDate(-1, 0, 0).Round(1 * time.Second).UTC()
    16  	path := filepath.Join(t.TempDir(), "file")
    17  	require.NoError(t, os.WriteFile(path, nil, 0o644))
    18  
    19  	require.NoError(t, Chtimes(path, fmt.Sprintf("%d", modTime.Unix())))
    20  
    21  	stat, err := os.Stat(path)
    22  	require.NoError(t, err)
    23  	require.True(t, modTime.Equal(stat.ModTime()))
    24  }
    25  
    26  func TestChtimesFileDoesNotExist(t *testing.T) {
    27  	modTime := time.Now().AddDate(-1, 0, 0).Round(1 * time.Second).UTC()
    28  	path := filepath.Join(t.TempDir(), "file")
    29  
    30  	require.ErrorIs(t, Chtimes(path, fmt.Sprintf("%d", modTime.Unix())), os.ErrNotExist)
    31  }
    32  
    33  func TestChtimesInvalidTS(t *testing.T) {
    34  	path := filepath.Join(t.TempDir(), "file")
    35  	require.NoError(t, os.WriteFile(path, nil, 0o644))
    36  
    37  	require.ErrorIs(t, Chtimes(path, "fake"), strconv.ErrSyntax)
    38  }
    39  
    40  func TestChtimesEmpty(t *testing.T) {
    41  	modTime := time.Now().AddDate(-1, 0, 0).Round(1 * time.Second).UTC()
    42  	path := filepath.Join(t.TempDir(), "file")
    43  	require.NoError(t, os.WriteFile(path, nil, 0o644))
    44  
    45  	require.NoError(t, Chtimes(path, ""))
    46  
    47  	stat, err := os.Stat(path)
    48  	require.NoError(t, err)
    49  	require.False(t, modTime.Equal(stat.ModTime()))
    50  }