summaryrefslogtreecommitdiff
path: root/describe/describe_test.go
blob: c1bb3187bde551b60283e2f061f7cf0ebfe149c6 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//go:build test

package describe

import (
	"github.com/ewy1/pik/runner"
	"github.com/stretchr/testify/assert"
	"os"
	"path/filepath"
	"testing"
)

type key struct {
	*runner.Stub
}

func Key() *key {
	return &key{
		Stub: &runner.Stub{},
	}
}

func TestDescribe(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "file")
	err := os.WriteFile(f, []byte("# this is a comment"), 0666)
	assert.NoError(t, err)
	result, err := Describe(Key(), f)
	assert.NoError(t, err)
	assert.Contains(t, result, "this is a comment")
}

func TestDescribe_Slashes(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "file")
	err := os.WriteFile(f, []byte(" // this is a comment"), 0666)
	assert.NoError(t, err)
	result, err := Describe(Key(), f)
	assert.NoError(t, err)
	assert.Contains(t, result, "this is a comment")
}

func TestDescribe_Cache(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "file")
	k := Key()
	err := os.WriteFile(f, []byte(" // this is a comment"), 0666)
	assert.NoError(t, err)
	result, err := Describe(k, f)
	assert.NoError(t, err)
	assert.Contains(t, result, "this is a comment")
	err = os.Remove(f)
	if err != nil {
		assert.NoError(t, err)
	}
	cached, err := Describe(k, f)
	assert.NoError(t, err)
	assert.Equal(t, result, cached)

}

func TestDescribe_NoDescription(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "file")
	err := os.WriteFile(f, []byte("1230987324091823740918237409283"), 0666)
	assert.NoError(t, err)
	result, err := Describe(Key(), f)
	assert.NoError(t, err)
	assert.Empty(t, result)
}

func TestDescribe_Shebang_NoDescription(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "file")
	err := os.WriteFile(f, []byte("#!/usr/bin/env bash\nkjausdhuahsdf"), 0666)
	assert.NoError(t, err)
	result, err := Describe(Key(), f)
	assert.Empty(t, result)
	assert.NoError(t, err)
}

func TestDescribe_Error(t *testing.T) {
	dir := t.TempDir()
	f := filepath.Join(dir, "file")
	result, err := Describe(Key(), f)
	assert.Empty(t, result)
	assert.Error(t, err)
}