git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/singleinstance/singleinstance_windows_test.go (about)

     1  package singleinstance_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"git.sr.ht/~pingoo/stdx/singleinstance"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestSingle(t *testing.T) {
    13  	s, err := singleinstance.New("unittest")
    14  	require.NoError(t, err)
    15  	require.NotNil(t, s)
    16  
    17  	t.Logf("Lockfile: %s", s.Lockfile())
    18  
    19  	err = s.Lock()
    20  	assert.NoError(t, err)
    21  
    22  	assert.EqualError(t, checkLock(s), singleinstance.ErrAlreadyRunning.Error())
    23  
    24  	err = s.Unlock()
    25  	assert.NoError(t, err)
    26  }
    27  
    28  func checkLock(s *singleinstance.Single) error {
    29  	if err := os.Remove(s.Lockfile()); err != nil && !os.IsNotExist(err) {
    30  		return singleinstance.ErrAlreadyRunning
    31  	}
    32  
    33  	return nil
    34  }