aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/util/io_test.go
blob: 0b2ad0f081b8c9c6dad56e4564e0ac76c696ba7d (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package util

import (
	"os"
	"testing"

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

func TestPathExists(t *testing.T) {
	assert.Equal(t, true, PathExists("io.go"))
	assert.Equal(t, false, PathExists("nosuchthing.go"))

	tmpDir := t.TempDir()
	err := os.MkdirAll(tmpDir+"/no", 0755)
	assert.Nil(t, err)
	err = os.MkdirAll(tmpDir+"/no/such", 0)
	assert.Nil(t, err)
	assert.Equal(t, false, PathExists(tmpDir+"/no/such/thing.go"))
}

func TestIsDir(t *testing.T) {
	tmpDir := t.TempDir()
	err := os.MkdirAll(tmpDir+"/no", 0755)
	assert.Nil(t, err)
	assert.Equal(t, true, IsDirectory(tmpDir+"/no"))
	err = os.MkdirAll(tmpDir+"/no/such", 0)
	assert.Nil(t, err)
	assert.Equal(t, true, IsDirectory(tmpDir+"/no/such"))
	assert.Equal(t, false, IsDirectory(tmpDir+"/no/such/thing.go"))
}

func TestIsRegularFile(t *testing.T) {
	assert.Equal(t, true, IsRegularFile("io.go"))
	assert.Equal(t, false, IsRegularFile("."))
	tmpDir := t.TempDir()
	err := os.MkdirAll(tmpDir+"/no", 0755)
	assert.Nil(t, err)
	err = os.MkdirAll(tmpDir+"/no/such", 0)
	assert.Nil(t, err)
	assert.Equal(t, false, IsRegularFile(tmpDir+"/no/such/thing.go"))
}

func TestIsExecutableFile(t *testing.T) {
	assert.Equal(t, false, IsExecutableFile("io.go"))
	assert.Equal(t, false, IsExecutableFile("nosuchthing.go"))
	tmpDir := t.TempDir()
	err := os.WriteFile(tmpDir+"/run.sh", []byte("#!/bin/sh\necho foo\n"), 0755)
	assert.Nil(t, err)
	assert.Equal(t, true, IsExecutableFile(tmpDir+"/run.sh"))
	/* unix only:
	out, err := BackTicksWithStderr.Run(tmpDir + "/run.sh")
	assert.Nil(t, err)
	assert.Equal(t, "foo\n", out)
	*/
}