Skip to content

Commit 3408445

Browse files
committed
Simplified RunCTags / factored test subroutine
1 parent 5875f09 commit 3408445

File tree

3 files changed

+25
-58
lines changed

3 files changed

+25
-58
lines changed

legacy/builder/container_add_prototypes.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ func PreprocessSketchWithCtags(ctx *types.Context) error {
6262
ctx.SketchSourceAfterCppPreprocessing = filterSketchSource(ctx.Sketch, bytes.NewReader(src), false)
6363
}
6464

65-
ctagsStdout, ctagsStderr, err := RunCTags(
66-
ctx.SketchSourceAfterCppPreprocessing, "sketch_merged.cpp", ctx.BuildProperties, ctx.PreprocPath)
65+
if err := targetFilePath.WriteFile([]byte(ctx.SketchSourceAfterCppPreprocessing)); err != nil {
66+
return err
67+
}
68+
69+
ctagsStdout, ctagsStderr, err := RunCTags(targetFilePath, ctx.BuildProperties)
6770
if ctx.Verbose {
6871
ctx.WriteStderr(ctagsStderr)
6972
}

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: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,34 @@ import (
2121

2222
bldr "github.com/arduino/arduino-cli/arduino/builder"
2323
"github.com/arduino/arduino-cli/legacy/builder"
24-
"github.com/arduino/arduino-cli/legacy/builder/types"
2524
paths "github.com/arduino/go-paths-helper"
2625
"github.com/stretchr/testify/require"
2726
)
2827

29-
func TestCTagsRunner(t *testing.T) {
30-
sketchLocation := Abs(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"))
28+
func ctagsRunnerTestTemplate(t *testing.T, sketchLocation *paths.Path) []byte {
3129
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
3230
defer cleanUpBuilderTestContext(t, ctx)
3331
ctx.Verbose = true
3432

3533
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
3634
NoError(t, err)
35+
3736
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, nil, ctx.SketchBuildPath)
3837
NoError(t, err)
39-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
38+
39+
target := ctx.BuildPath.Join("ctags_target.cpp")
40+
NoError(t, target.WriteFile([]byte(source)))
41+
42+
ctagsOutput, _, err := builder.RunCTags(target, ctx.BuildProperties)
4043
NoError(t, err)
4144

45+
return ctagsOutput
46+
}
47+
48+
func TestCTagsRunner(t *testing.T) {
49+
sketchLocation := Abs(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"))
50+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
51+
4252
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
4353
expectedOutput := "server " + quotedSketchLocation + " /^BridgeServer server;$/;\" kind:variable line:31\n" +
4454
"setup " + quotedSketchLocation + " /^void setup() {$/;\" kind:function line:33 signature:() returntype:void\n" +
@@ -52,16 +62,7 @@ func TestCTagsRunner(t *testing.T) {
5262

5363
func TestCTagsRunnerSketchWithClass(t *testing.T) {
5464
sketchLocation := Abs(t, paths.New("sketch_with_class", "sketch_with_class.ino"))
55-
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
56-
defer cleanUpBuilderTestContext(t, ctx)
57-
ctx.Verbose = true
58-
59-
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
60-
NoError(t, err)
61-
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
62-
NoError(t, err)
63-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
64-
NoError(t, err)
65+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
6566

6667
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
6768
expectedOutput := "set_values\t" + quotedSketchLocation + "\t/^ void set_values (int,int);$/;\"\tkind:prototype\tline:4\tclass:Rectangle\tsignature:(int,int)\treturntype:void\n" +
@@ -74,16 +75,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) {
7475

7576
func TestCTagsRunnerSketchWithTypename(t *testing.T) {
7677
sketchLocation := Abs(t, paths.New("sketch_with_typename", "sketch_with_typename.ino"))
77-
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
78-
defer cleanUpBuilderTestContext(t, ctx)
79-
ctx.Verbose = true
80-
81-
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
82-
NoError(t, err)
83-
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
84-
NoError(t, err)
85-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
86-
NoError(t, err)
78+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
8779

8880
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
8981
expectedOutput := "Foo\t" + quotedSketchLocation + "\t/^ struct Foo{$/;\"\tkind:struct\tline:2\n" +
@@ -95,16 +87,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) {
9587

9688
func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
9789
sketchLocation := Abs(t, paths.New("sketch_with_namespace", "sketch_with_namespace.ino"))
98-
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
99-
defer cleanUpBuilderTestContext(t, ctx)
100-
ctx.Verbose = true
101-
102-
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
103-
NoError(t, err)
104-
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
105-
NoError(t, err)
106-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
107-
NoError(t, err)
90+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
10891

10992
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
11093
expectedOutput := "value\t" + quotedSketchLocation + "\t/^\tint value() {$/;\"\tkind:function\tline:2\tnamespace:Test\tsignature:()\treturntype:int\n" +
@@ -115,16 +98,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
11598

11699
func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
117100
sketchLocation := Abs(t, paths.New("sketch_with_templates_and_shift", "sketch_with_templates_and_shift.ino"))
118-
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
119-
defer cleanUpBuilderTestContext(t, ctx)
120-
ctx.Verbose = true
121-
122-
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
123-
NoError(t, err)
124-
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
125-
NoError(t, err)
126-
ctagsOutput, _, err := builder.RunCTags(source, "ctags_target.cpp", ctx.BuildProperties, ctx.PreprocPath)
127-
NoError(t, err)
101+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
128102

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

0 commit comments

Comments
 (0)