Skip to content

Commit a322ac9

Browse files
committed
Simplified RunCTags / factored test subroutine
1 parent bf2f521 commit a322ac9

File tree

3 files changed

+26
-106
lines changed

3 files changed

+26
-106
lines changed

legacy/builder/container_add_prototypes.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ func PreprocessSketchWithCtags(ctx *types.Context) error {
5656
ctx.SketchSourceAfterCppPreprocessing = bldr.FilterSketchSource(ctx.Sketch, bytes.NewReader(src), false)
5757
}
5858

59-
ctagsStdout, ctagsStderr, err := RunCTags(
60-
ctx.SketchSourceAfterCppPreprocessing, "sketch_merged.cpp", ctx.BuildProperties, ctx.PreprocPath)
59+
if err := targetFilePath.WriteFile([]byte(ctx.SketchSourceAfterCppPreprocessing)); err != nil {
60+
return err
61+
}
62+
63+
ctagsStdout, ctagsStderr, err := RunCTags(targetFilePath, ctx.BuildProperties)
6164
if ctx.Verbose {
6265
ctx.WriteStderr(ctagsStderr)
6366
}

legacy/builder/ctags_runner.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,14 @@ import (
2525
"github.com/pkg/errors"
2626
)
2727

28-
func RunCTags(source string, targetFileName string, buildProperties *properties.Map, preprocPath *paths.Path,
29-
) ([]byte, []byte, error) {
30-
if err := preprocPath.MkdirAll(); err != nil {
31-
return nil, nil, err
32-
}
33-
34-
ctagsTargetFilePath := preprocPath.Join(targetFileName)
35-
if err := ctagsTargetFilePath.WriteFile([]byte(source)); err != nil {
36-
return nil, nil, err
37-
}
38-
28+
func RunCTags(sourceFile *paths.Path, buildProperties *properties.Map) ([]byte, []byte, error) {
3929
ctagsBuildProperties := properties.NewMap()
4030
ctagsBuildProperties.Set("tools.ctags.path", "{runtime.tools.ctags.path}")
4131
ctagsBuildProperties.Set("tools.ctags.cmd.path", "{path}/ctags")
4232
ctagsBuildProperties.Set("tools.ctags.pattern", `"{cmd.path}" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "{source_file}"`)
4333
ctagsBuildProperties.Merge(buildProperties)
4434
ctagsBuildProperties.Merge(ctagsBuildProperties.SubTree("tools").SubTree("ctags"))
45-
ctagsBuildProperties.SetPath("source_file", ctagsTargetFilePath)
35+
ctagsBuildProperties.SetPath("source_file", sourceFile)
4636

4737
pattern := ctagsBuildProperties.Get("pattern")
4838
if pattern == "" {

legacy/builder/test/ctags_runner_test.go

Lines changed: 19 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ import (
2727
"github.com/stretchr/testify/require"
2828
)
2929

30-
func TestCTagsRunner(t *testing.T) {
30+
func ctagsRunnerTestTemplate(t *testing.T, sketchLocation *paths.Path) []byte {
3131
DownloadCoresAndToolsAndLibraries(t)
3232

33-
sketchLocation := Abs(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"))
34-
3533
ctx := &types.Context{
3634
HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"),
3735
BuiltInToolsDirs: paths.NewPathList("downloaded_tools"),
@@ -47,11 +45,24 @@ func TestCTagsRunner(t *testing.T) {
4745

4846
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
4947
NoError(t, err)
48+
5049
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, nil, ctx.SketchBuildPath)
5150
NoError(t, err)
52-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
51+
52+
target := ctx.BuildPath.Join("ctags_target.cpp")
53+
NoError(t, target.Parent().MkdirAll())
54+
NoError(t, target.WriteFile([]byte(source)))
55+
56+
ctagsOutput, _, err := builder.RunCTags(target, ctx.BuildProperties)
5357
NoError(t, err)
5458

59+
return ctagsOutput
60+
}
61+
62+
func TestCTagsRunner(t *testing.T) {
63+
sketchLocation := Abs(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"))
64+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
65+
5566
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
5667
expectedOutput := "server " + quotedSketchLocation + " /^BridgeServer server;$/;\" kind:variable line:31\n" +
5768
"setup " + quotedSketchLocation + " /^void setup() {$/;\" kind:function line:33 signature:() returntype:void\n" +
@@ -64,29 +75,8 @@ func TestCTagsRunner(t *testing.T) {
6475
}
6576

6677
func TestCTagsRunnerSketchWithClass(t *testing.T) {
67-
DownloadCoresAndToolsAndLibraries(t)
68-
6978
sketchLocation := Abs(t, paths.New("sketch_with_class", "sketch_with_class.ino"))
70-
71-
ctx := &types.Context{
72-
HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"),
73-
BuiltInToolsDirs: paths.NewPathList("downloaded_tools"),
74-
BuiltInLibrariesDirs: paths.New("downloaded_libraries"),
75-
OtherLibrariesDirs: paths.NewPathList("libraries"),
76-
Sketch: OpenSketch(t, sketchLocation),
77-
FQBN: parseFQBN(t, "arduino:avr:leonardo"),
78-
Verbose: true,
79-
}
80-
81-
buildPath := SetupBuildPath(t, ctx)
82-
defer buildPath.RemoveAll()
83-
84-
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
85-
NoError(t, err)
86-
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
87-
NoError(t, err)
88-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
89-
NoError(t, err)
79+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
9080

9181
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
9282
expectedOutput := "set_values\t" + quotedSketchLocation + "\t/^ void set_values (int,int);$/;\"\tkind:prototype\tline:4\tclass:Rectangle\tsignature:(int,int)\treturntype:void\n" +
@@ -98,29 +88,8 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) {
9888
}
9989

10090
func TestCTagsRunnerSketchWithTypename(t *testing.T) {
101-
DownloadCoresAndToolsAndLibraries(t)
102-
10391
sketchLocation := Abs(t, paths.New("sketch_with_typename", "sketch_with_typename.ino"))
104-
105-
ctx := &types.Context{
106-
HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"),
107-
BuiltInToolsDirs: paths.NewPathList("downloaded_tools"),
108-
BuiltInLibrariesDirs: paths.New("downloaded_libraries"),
109-
OtherLibrariesDirs: paths.NewPathList("libraries"),
110-
Sketch: OpenSketch(t, sketchLocation),
111-
FQBN: parseFQBN(t, "arduino:avr:leonardo"),
112-
Verbose: true,
113-
}
114-
115-
buildPath := SetupBuildPath(t, ctx)
116-
defer buildPath.RemoveAll()
117-
118-
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
119-
NoError(t, err)
120-
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
121-
NoError(t, err)
122-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
123-
NoError(t, err)
92+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
12493

12594
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
12695
expectedOutput := "Foo\t" + quotedSketchLocation + "\t/^ struct Foo{$/;\"\tkind:struct\tline:2\n" +
@@ -131,29 +100,8 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) {
131100
}
132101

133102
func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
134-
DownloadCoresAndToolsAndLibraries(t)
135-
136103
sketchLocation := Abs(t, paths.New("sketch_with_namespace", "sketch_with_namespace.ino"))
137-
138-
ctx := &types.Context{
139-
HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"),
140-
BuiltInToolsDirs: paths.NewPathList("downloaded_tools"),
141-
BuiltInLibrariesDirs: paths.New("downloaded_libraries"),
142-
OtherLibrariesDirs: paths.NewPathList("libraries"),
143-
Sketch: OpenSketch(t, sketchLocation),
144-
FQBN: parseFQBN(t, "arduino:avr:leonardo"),
145-
Verbose: true,
146-
}
147-
148-
buildPath := SetupBuildPath(t, ctx)
149-
defer buildPath.RemoveAll()
150-
151-
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
152-
NoError(t, err)
153-
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
154-
NoError(t, err)
155-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
156-
NoError(t, err)
104+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
157105

158106
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
159107
expectedOutput := "value\t" + quotedSketchLocation + "\t/^\tint value() {$/;\"\tkind:function\tline:2\tnamespace:Test\tsignature:()\treturntype:int\n" +
@@ -163,29 +111,8 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
163111
}
164112

165113
func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
166-
DownloadCoresAndToolsAndLibraries(t)
167-
168114
sketchLocation := Abs(t, paths.New("sketch_with_templates_and_shift", "sketch_with_templates_and_shift.ino"))
169-
170-
ctx := &types.Context{
171-
HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"),
172-
BuiltInToolsDirs: paths.NewPathList("downloaded_tools"),
173-
BuiltInLibrariesDirs: paths.New("downloaded_libraries"),
174-
OtherLibrariesDirs: paths.NewPathList("libraries"),
175-
Sketch: OpenSketch(t, sketchLocation),
176-
FQBN: parseFQBN(t, "arduino:avr:leonardo"),
177-
Verbose: true,
178-
}
179-
180-
buildPath := SetupBuildPath(t, ctx)
181-
defer buildPath.RemoveAll()
182-
183-
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
184-
NoError(t, err)
185-
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
186-
NoError(t, err)
187-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
188-
NoError(t, err)
115+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
189116

190117
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
191118
expectedOutput := "printGyro\t" + quotedSketchLocation + "\t/^void printGyro()$/;\"\tkind:function\tline:10\tsignature:()\treturntype:void\n" +

0 commit comments

Comments
 (0)