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
89
90
91
92
93
94
95
96
97
98
99
100
|
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"path/filepath"
"testing"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
)
func abs(t *testing.T, path string) string {
t.Helper()
absPath, err := filepath.Abs(path)
require.NoError(t, err)
return absPath
}
func TestDeterminePathToSourceFromSourceMap(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()
testCases := []struct {
name string
sourceFile string
outFile string
expectedPath string
}{
{
name: "same directory",
sourceFile: filepath.FromSlash("data/program.teal"),
outFile: filepath.FromSlash("data/program.teal.tok"),
expectedPath: "program.teal",
},
{
name: "output one level up",
sourceFile: filepath.FromSlash("data/program.teal"),
outFile: filepath.FromSlash("data/output/program.teal.tok"),
expectedPath: filepath.FromSlash("../program.teal"),
},
{
name: "output one level down",
sourceFile: filepath.FromSlash("data/program.teal"),
outFile: "program.teal.tok",
expectedPath: filepath.FromSlash("data/program.teal"),
},
{
name: "input stdin",
sourceFile: stdinFileNameValue,
outFile: "program.teal.tok",
expectedPath: "<stdin>",
},
{
name: "output stdout",
sourceFile: filepath.FromSlash("data/program.teal"),
outFile: stdoutFilenameValue,
expectedPath: abs(t, filepath.FromSlash("data/program.teal")),
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
sources := []string{tc.sourceFile}
if tc.sourceFile != stdinFileNameValue {
sources = append(sources, abs(t, tc.sourceFile))
}
outs := []string{tc.outFile}
if tc.outFile != stdoutFilenameValue {
outs = append(outs, abs(t, tc.outFile))
}
for sourceIndex, source := range sources {
for outIndex, out := range outs {
actualPath, err := determinePathToSourceFromSourceMap(source, out)
require.NoError(t, err, "sourceIndex: %d, outIndex: %d", sourceIndex, outIndex)
require.Equal(t, tc.expectedPath, actualPath, "sourceIndex: %d, outIndex: %d", sourceIndex, outIndex)
}
}
})
}
}
|