summaryrefslogtreecommitdiffstats
path: root/client/go/internal/ioutil/ioutil_test.go
blob: 907132c9eaaf5c56fc99f7c29d35cc656a0c245e (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 ioutil

import (
	"os"
	"testing"

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

func TestPathExists(t *testing.T) {
	assert.Equal(t, true, Exists("ioutil.go"))
	assert.Equal(t, false, Exists("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, Exists(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, IsDir(tmpDir+"/no"))
	err = os.MkdirAll(tmpDir+"/no/such", 0)
	assert.Nil(t, err)
	assert.Equal(t, true, IsDir(tmpDir+"/no/such"))
	assert.Equal(t, false, IsDir(tmpDir+"/no/such/thing.go"))
}

func TestIsRegularFile(t *testing.T) {
	assert.Equal(t, true, IsFile("ioutil.go"))
	assert.Equal(t, false, IsFile("."))
	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, IsFile(tmpDir+"/no/such/thing.go"))
}

func TestIsExecutableFile(t *testing.T) {
	assert.Equal(t, false, IsExecutable("io.go"))
	assert.Equal(t, false, IsExecutable("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, IsExecutable(tmpDir+"/run.sh"))
	/* unix only:
	out, err := BackTicksWithStderr.Run(tmpDir + "/run.sh")
	assert.Nil(t, err)
	assert.Equal(t, "foo\n", out)
	*/
}