Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cover: use CamelCase for coverage variable #2984

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion go/private/actions/cover.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def _sanitize(s):
"""Replaces /, -, and . with _."""
return s.replace("/", "_").replace("-", "_").replace(".", "_")

# convert string from snake_case to CamelCase
def _snake_to_camel(s):
words = s.split("_")
out = ""
for w in words:
out += w[:1].upper()
out += w[1:].lower()
return out

def emit_cover(go, source):
"""See go/toolchains.rst#cover for full documentation."""

Expand All @@ -44,7 +53,8 @@ def emit_cover(go, source):
_, pkgpath = effective_importpath_pkgpath(source.library)
srcname = pkgpath + "/" + orig.basename if pkgpath else orig.path

cover_var = "Cover_%s_%s" % (_sanitize(pkgpath), _sanitize(src.basename[:-3]))
snake_cover_var = "Cover_%s_%s" % (_sanitize(pkgpath), _sanitize(src.basename[:-3]))
cover_var = _snake_to_camel(snake_cover_var)
out = go.declare_file(go, path = "Cover_%s" % _sanitize(src.basename[:-3]), ext = ".cover.go")
covered_src_map.pop(src, None)
covered_src_map[out] = orig
Expand Down
15 changes: 14 additions & 1 deletion go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ func compileArchive(
if ext := filepath.Ext(stem); ext != "" {
stem = stem[:len(stem)-len(ext)]
}
coverVar := fmt.Sprintf("Cover_%s_%d_%s", sanitizePathForIdentifier(importPath), i, sanitizePathForIdentifier(stem))
snakeCoverVar := fmt.Sprintf("Cover_%s_%d_%s", sanitizePathForIdentifier(importPath), i, sanitizePathForIdentifier(stem))
coverVar := snakeToCamel(snakeCoverVar)
coverSrc := filepath.Join(workDir, fmt.Sprintf("cover_%d.go", i))
if err := instrumentForCoverage(goenv, origSrc, srcName, coverVar, coverMode, coverSrc); err != nil {
return err
Expand Down Expand Up @@ -522,3 +523,15 @@ func sanitizePathForIdentifier(path string) string {
return '_'
}, path)
}

// snakeToCamel converts a snake_case string to CamelCase string
func snakeToCamel(s string) string {
words := strings.Split(s, "_")

result := ""
for _, w := range words {
result += strings.ToUpper(w[:1]) + strings.ToLower(w[1:])
}

return result
}