• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jonasbn / punycode / 20602582625

30 Dec 2025 05:51PM UTC coverage: 70.769% (-7.9%) from 78.689%
20602582625

push

github

web-flow
Merge pull request #100 from jonasbn/go_idna

Added some debug sections, more tests and cleaned the code

7 of 12 new or added lines in 1 file covered. (58.33%)

1 existing line in 1 file now uncovered.

46 of 65 relevant lines covered (70.77%)

6.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.77
/punycode.go
1
// punycode is a utility to encode and decode to and from punycode on the command line
2
package main
3

4
import (
5
        "bufio"
6
        "fmt"
7
        "io"
8
        "log"
9
        "os"
10
        "regexp"
11

12
        "golang.org/x/net/idna"
13
)
14

15
// punycodePrefix is a compiled regex pattern to match the punycode prefix "xn--"
16
var punycodePrefix = regexp.MustCompile("^xn--")
17

18
// profile is the IDNA profile used for punycode conversions, initialized once at package level.
19
// Using idna.New() creates a profile with default options suitable for general-purpose
20
// bidirectional conversion between Unicode and ASCII (punycode) representations.
21
var profile = idna.New()
22

23
// main function is a wrapper on the realMain function and emits OS exit code based on wrapped function
24
func main() {
×
25
        os.Exit(realMain())
×
26
}
×
27

28
// realMain function wrapped so it is testable, can read arguments from CLI og STDING returns integer, which can be translated into OS exit code
29
func realMain() int {
9✔
30
        argsWithoutProg := os.Args[1:]
9✔
31

9✔
32
        var outputString string
9✔
33

9✔
34
        if len(argsWithoutProg) <= 0 {
9✔
35
                var err error
×
36

×
37
                outputString, err = readStdin(os.Stdin)
×
38

×
39
                if err != nil {
×
40
                        log.Println(err)
×
41
                        return 2
×
42
                }
×
43

44
        } else {
9✔
45
                outputString = readArgs()
9✔
46
        }
9✔
47

48
        if outputString != "" {
16✔
49
                fmt.Printf("%s\n", outputString)
7✔
50
                return 0
7✔
51
        } else {
9✔
52
                return 1
2✔
53
        }
2✔
54
}
55

56
func readArgs() string {
9✔
57
        inputString := os.Args[1] // we only take a single parameter, the string to process
9✔
58

9✔
59
        return convertString(inputString)
9✔
60
}
9✔
61

62
func readStdin(stdin io.Reader) (string, error) {
6✔
63
        scanner := bufio.NewScanner(stdin)
6✔
64

6✔
65
        var inputString string
6✔
66

6✔
67
        for scanner.Scan() {
11✔
68
                inputString = scanner.Text()
5✔
69
        }
5✔
70

71
        if err := scanner.Err(); err != nil {
6✔
72
                return "", err
×
73
        }
×
74

75
        return convertString(inputString), nil
6✔
76
}
77

78
func convertString(inputString string) string {
15✔
79

15✔
80
        var outputString string
15✔
81
        var err error
15✔
82

15✔
83
        match := punycodePrefix.MatchString(inputString)
15✔
84

15✔
85
        if match {
21✔
86
                outputString, err = profile.ToUnicode(inputString)
6✔
87

6✔
88
                if err != nil {
6✔
NEW
89
                        log.Println(err)
×
NEW
90
                        return ""
×
UNCOV
91
                }
×
92

93
        } else {
9✔
94
                outputString, err = profile.ToASCII(inputString)
9✔
95

9✔
96
                if err != nil {
9✔
NEW
97
                        log.Println(err)
×
NEW
98
                        return ""
×
NEW
99
                }
×
100
        }
101

102
        return outputString
15✔
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc