forked from ixmilia/dxf-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrationTestHelpers.go
147 lines (126 loc) · 2.81 KB
/
integrationTestHelpers.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package dxf
import (
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
)
type odaHelper struct {
OdaPath string
TempPath string
InputPath string
OutputPath string
}
func newOdaHelper() (helper *odaHelper, err error) {
odaPath, err := getOdaPath()
if err != nil {
return
}
tempPath, err := ioutil.TempDir("", "OdaIntegrationTest")
if err != nil {
return
}
inputPath := filepath.Join(tempPath, "input")
outputPath := filepath.Join(tempPath, "output")
err = os.MkdirAll(inputPath, os.ModePerm)
if err != nil {
return
}
err = os.MkdirAll(outputPath, os.ModePerm)
if err != nil {
return
}
helper = &odaHelper{
OdaPath: odaPath,
TempPath: tempPath,
InputPath: inputPath,
OutputPath: outputPath,
}
return
}
func odaVersionString(t *testing.T, v AcadVersion) string {
switch v {
case R9:
return "ACAD9"
case R10:
return "ACAD10"
case R12:
return "ACAD12"
case R13:
return "ACAD13"
case R14:
return "ACAD14"
case R2000:
return "ACAD2000"
case R2004:
return "ACAD2004"
case R2007:
return "ACAD2007"
case R2010:
return "ACAD2010"
case R2013:
return "ACAD2013"
case R2018:
return "ACAD2018"
default:
t.Errorf("Unsupported drawing version: %s", v)
return "UNSUPPORTED"
}
}
func (o *odaHelper) convertDrawing(t *testing.T, d *Drawing, v AcadVersion) (result Drawing) {
err := d.SaveFile(filepath.Join(o.InputPath, "drawing.dxf"))
if err != nil {
t.Fatalf("Unable to save drawing to temporarly location %v", o.InputPath)
}
args := []string{
o.InputPath,
o.OutputPath,
odaVersionString(t, v),
"DXF", // output file type
"0", // recurse
"1", // audit
}
cmdErr := exec.Command(o.OdaPath, args...).Run()
errors := ""
errorFiles, _ := filepath.Glob(o.OutputPath)
for _, errorFile := range errorFiles {
bytes, _ := ioutil.ReadFile(errorFile)
errors += string(bytes)
}
if len(errors) > 0 {
t.Fatalf("Error converting files:\n%v", errors)
}
if cmdErr != nil {
t.Fatal("Unable to convert drawing file")
}
result, err = ReadFile(filepath.Join(o.OutputPath, "drawing.dxf"))
if err != nil {
t.Fatalf("Unable to re-read converted drawing: %v", err)
}
return
}
func (o *odaHelper) cleanup(t *testing.T) {
if t.Failed() {
t.Logf("Temporary file location: %v", o.TempPath)
} else {
os.RemoveAll(o.TempPath)
}
}
func getOdaPath() (odaPath string, err error) {
// Find ODA converter. Final path looks something like:
// C:\Program Files\ODA\ODAFileConverter_title 20.12.0\ODAFileConverter.exe
matches, err := filepath.Glob("C:/Program Files/ODA/ODAFileConverter*/ODAFileConverter.exe")
if err != nil {
// don't really care about the error, just quit
return
}
if len(matches) == 0 {
// nothing found, don't care
err = errors.New("non-fatal, no ODA converter found")
return
}
odaPath = matches[0]
return
}