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

jonasbn / punycode / 20600524784

30 Dec 2025 03:58PM UTC coverage: 71.795% (-6.9%) from 78.689%
20600524784

Pull #100

github

web-flow
Update punycode.go

Suggestion from copilot, to handle possible conversion errors

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Pull Request #100: Added some debug sections, more tests and cleaned the code

17 of 25 new or added lines in 1 file covered. (68.0%)

1 existing line in 1 file now uncovered.

56 of 78 relevant lines covered (71.79%)

7.28 hits per line

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

71.79
/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
// main function is a wrapper on the realMain function and emits OS exit code based on wrapped function
16
func main() {
×
17
        os.Exit(realMain())
×
18
}
×
19

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

9✔
24
        var outputString string
9✔
25

9✔
26
        if len(argsWithoutProg) <= 0 {
9✔
27
                var err error
×
28

×
29
                outputString, err = readStdin(os.Stdin)
×
30

×
31
                if err != nil {
×
32
                        log.Println(err)
×
33
                        return 2
×
34
                }
×
35

36
        } else {
9✔
37
                outputString = readArgs()
9✔
38
        }
9✔
39

40
        if outputString != "" {
16✔
41
                fmt.Printf("%s\n", outputString)
7✔
42
                return 0
7✔
43
        } else {
9✔
44
                return 1
2✔
45
        }
2✔
46
}
47

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

9✔
51
        return convertString(inputString)
9✔
52
}
9✔
53

54
func readStdin(stdin io.Reader) (string, error) {
6✔
55
        scanner := bufio.NewScanner(stdin)
6✔
56

6✔
57
        var inputString string
6✔
58

6✔
59
        for scanner.Scan() {
11✔
60
                inputString = scanner.Text()
5✔
61
        }
5✔
62

63
        if err := scanner.Err(); err != nil {
6✔
64
                return "", err
×
65
        }
×
66

67
        return convertString(inputString), nil
6✔
68
}
69

70
func convertString(inputString string) string {
15✔
71

15✔
72
        var outputString string
15✔
73

15✔
74
        match, err := regexp.MatchString("^xn--", inputString)
15✔
75

15✔
76
        if err != nil {
15✔
NEW
77
                log.Println(err)
×
NEW
78
                return ""
×
NEW
79
        }
×
80

81
        var p *idna.Profile = idna.New()
15✔
82

15✔
83
        /* DEBUG OUTPUT
15✔
84
        fmt.Printf("Bytes: %v\n", []byte(inputString))
15✔
85
        fmt.Printf("Runes: %U\n", []rune(inputString))
15✔
86
        fmt.Printf("Length in bytes: %d, runes: %d\n", len(inputString), len([]rune(inputString)))
15✔
87
        fmt.Printf(inputString + "\n")
15✔
88
        */
15✔
89

15✔
90
        if match {
21✔
91
                unicodeString, err := p.ToUnicode(inputString)
6✔
92

6✔
93
                if err != nil {
6✔
NEW
94
                        log.Println(err)
×
NEW
95
                        return ""
×
UNCOV
96
                }
×
97
                outputString = unicodeString
6✔
98

99
        } else {
9✔
100
                outputString, err = p.ToASCII(inputString)
9✔
101

9✔
102
                if err != nil {
9✔
NEW
103
                        log.Println(err)
×
NEW
104
                        return ""
×
NEW
105
                }
×
106
        }
107

108
        /* DEBUG OUTPUT
109
        fmt.Printf("Bytes: %v\n", []byte(outputString))
110
        fmt.Printf("Runes: %U\n", []rune(outputString))
111
        fmt.Printf("Length in bytes: %d, runes: %d\n", len(outputString), len([]rune(outputString)))
112
        fmt.Printf(outputString + "\n")
113
        */
114

115
        return outputString
15✔
116
}
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