~mna/snow unlisted

ref: b80c93389bef70627bad6a5d2db7398350b0ef65 snow/cmd/internal/mainer/mainer_test.go -rw-r--r-- 817 bytes
b80c9338Martin Angers cmd/internal: add packages for command-line 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package mainer

import (
	"context"
	"os"
	"syscall"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestCurrentStdio(t *testing.T) {
	cwd, err := os.Getwd()
	require.NoError(t, err)
	require.Equal(t, cwd, CurrentStdio().Cwd)
}

func TestCancelOnSignal(t *testing.T) {
	ctx := context.Background()
	ctx = CancelOnSignal(ctx, syscall.SIGUSR1)

	select {
	case <-ctx.Done():
		require.Fail(t, "context should block")
	default:
	}

	proc, err := os.FindProcess(os.Getpid())
	require.NoError(t, err)
	require.NoError(t, proc.Signal(syscall.SIGUSR1))

	select {
	case <-ctx.Done():
	case <-time.After(time.Second):
		require.Fail(t, "context should be done")
	}
}

func TestCancelOnSignal_NoSignal(t *testing.T) {
	ctx := context.Background()
	ctx2 := CancelOnSignal(ctx)
	require.Equal(t, ctx, ctx2)
}