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

charmbracelet / charm / 13016148640

28 Jan 2025 05:31PM UTC coverage: 15.19% (-0.07%) from 15.256%
13016148640

Pull #308

github

web-flow
chore(deps): bump github.com/golang/glog

Bumps [github.com/golang/glog](https://github.com/golang/glog) from 0.0.0-20160126235308-23def4e6c14b to 1.2.4.
- [Release notes](https://github.com/golang/glog/releases)
- [Commits](https://github.com/golang/glog/commits/v1.2.4)

---
updated-dependencies:
- dependency-name: github.com/golang/glog
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #308: chore(deps): bump github.com/golang/glog from 0.0.0-20160126235308-23def4e6c14b to 1.2.4

924 of 6083 relevant lines covered (15.19%)

0.75 hits per line

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

73.42
/testserver/testserver.go
1
package testserver
2

3
import (
4
        "fmt"
5
        "net"
6
        "net/http"
7
        "os"
8
        "path/filepath"
9
        "strconv"
10
        "strings"
11
        "testing"
12
        "time"
13

14
        "github.com/charmbracelet/charm/client"
15
        "github.com/charmbracelet/charm/server"
16
        "github.com/charmbracelet/keygen"
17
)
18

19
// SetupTestServer starts a test server and sets the needed environment
20
// variables so clients pick it up.
21
// It also returns a client forcing these settings in.
22
// Unless you use the given client, this is not really thread safe due
23
// to setting a bunch of environment variables.
24
func SetupTestServer(tb testing.TB) *client.Client {
4✔
25
        tb.Helper()
4✔
26

4✔
27
        td := tb.TempDir()
4✔
28
        sp := filepath.Join(td, ".ssh")
4✔
29
        clientData := filepath.Join(td, ".client-data")
4✔
30

4✔
31
        cfg := server.DefaultConfig()
4✔
32
        cfg.DataDir = filepath.Join(td, ".data")
4✔
33
        cfg.SSHPort = randomPort(tb)
4✔
34
        cfg.HTTPPort = randomPort(tb)
4✔
35
        cfg.HealthPort = randomPort(tb)
4✔
36

4✔
37
        kp, err := keygen.New(filepath.Join(sp, "charm_server_ed25519"), keygen.WithKeyType(keygen.Ed25519), keygen.WithWrite())
4✔
38
        if err != nil {
4✔
39
                tb.Fatalf("keygen error: %s", err)
×
40
        }
×
41

42
        // TODO: see if this works the same
43
        cfg = cfg.WithKeys(kp.RawAuthorizedKey(), kp.RawPrivateKey())
4✔
44
        s, err := server.NewServer(cfg)
4✔
45
        if err != nil {
4✔
46
                tb.Fatalf("new server error: %s", err)
×
47
        }
×
48

49
        _ = os.Setenv("CHARM_HOST", cfg.Host)
4✔
50
        _ = os.Setenv("CHARM_SSH_PORT", fmt.Sprintf("%d", cfg.SSHPort))
4✔
51
        _ = os.Setenv("CHARM_HTTP_PORT", fmt.Sprintf("%d", cfg.HTTPPort))
4✔
52
        _ = os.Setenv("CHARM_DATA_DIR", clientData)
4✔
53

4✔
54
        go func() { _ = s.Start() }()
8✔
55

56
        resp, err := FetchURL(fmt.Sprintf("http://localhost:%d", cfg.HealthPort), 3)
4✔
57
        if err != nil {
4✔
58
                tb.Fatalf("server likely failed to start: %s", err)
×
59
        }
×
60
        defer resp.Body.Close() // nolint:errcheck
4✔
61

4✔
62
        tb.Cleanup(func() {
8✔
63
                if err := s.Close(); err != nil {
4✔
64
                        tb.Error("failed to close server:", err)
×
65
                }
×
66

67
                _ = os.Unsetenv("CHARM_HOST")
4✔
68
                _ = os.Unsetenv("CHARM_SSH_PORT")
4✔
69
                _ = os.Unsetenv("CHARM_HTTP_PORT")
4✔
70
                _ = os.Unsetenv("CHARM_DATA_DIR")
4✔
71
        })
72

73
        ccfg, err := client.ConfigFromEnv()
4✔
74
        if err != nil {
4✔
75
                tb.Fatalf("client config from env error: %s", err)
×
76
        }
×
77

78
        ccfg.Host = cfg.Host
4✔
79
        ccfg.SSHPort = cfg.SSHPort
4✔
80
        ccfg.HTTPPort = cfg.HTTPPort
4✔
81
        ccfg.DataDir = clientData
4✔
82

4✔
83
        cl, err := client.NewClient(ccfg)
4✔
84
        if err != nil {
4✔
85
                tb.Fatalf("new client error: %s", err)
×
86
        }
×
87
        return cl
4✔
88
}
89

90
// Fetch the given URL with N retries.
91
func FetchURL(url string, retries int) (*http.Response, error) {
4✔
92
        resp, err := http.Get(url) // nolint:gosec
4✔
93
        if err != nil {
4✔
94
                if retries > 0 {
×
95
                        time.Sleep(time.Second)
×
96
                        return FetchURL(url, retries-1)
×
97
                }
×
98
                return nil, err
×
99
        }
100
        if resp.StatusCode != 200 {
4✔
101
                return resp, fmt.Errorf("bad http status code: %d", resp.StatusCode)
×
102
        }
×
103
        return resp, nil
4✔
104
}
105

106
func randomPort(tb testing.TB) int {
12✔
107
        listener, err := net.Listen("tcp", "127.0.0.1:0")
12✔
108
        if err != nil {
12✔
109
                tb.Fatalf("could not get a random port: %s", err)
×
110
        }
×
111
        listener.Close() //nolint:errcheck
12✔
112

12✔
113
        addr := listener.Addr().String()
12✔
114

12✔
115
        p, _ := strconv.Atoi(addr[strings.LastIndex(addr, ":")+1:])
12✔
116
        return p
12✔
117
}
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

© 2025 Coveralls, Inc