-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
75 lines (60 loc) · 1.65 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2021 James Bowes. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package whatsnew_test
import (
"context"
"fmt"
"net/http"
"time"
"github.com/jbowes/whatsnew"
"github.com/jbowes/whatsnew/impl"
)
func Example() {
ctx := context.Background()
fut := whatsnew.Check(ctx, &whatsnew.Options{
Slug: "you/your-app",
Cache: "testdata/update-cache.json",
Version: "v0.0.1",
})
// Run your CLI code and whatnot
if v, _ := fut.Get(); v != "" {
fmt.Printf("new release available: %s\n", v)
}
// Output:
// new release available: v0.2.0
}
// This example test isn't really needed, but it keeps the file
// from being an example program, so we can replace the http
// transport etc.
// Set a custom check frequency of 24 hours.
func Example_customFrequency() {
ctx := context.Background()
fut := whatsnew.Check(ctx, &whatsnew.Options{
Slug: "you/your-app",
Cache: "testdata/update-cache.json",
Version: "0.0.1",
Frequency: 24 * time.Hour,
})
// Run your CLI code and whatnot
if v, _ := fut.Get(); v != "" {
fmt.Printf("new release available: %s\n", v)
}
// Output:
// new release available: 0.30.0
}
func init() {
ctx := context.Background()
// setup. write out a cache that will hit for one test and
// miss on the other
cache := impl.FileCacher{Path: "testdata/update-cache.json"}
_ = cache.Set(ctx, &impl.Info{
Version: "v0.2.0",
CheckTime: time.Now().Add(-25 * time.Hour),
Etag: "whatever",
})
// replace http default transport.
http.DefaultTransport = http.NewFileTransport(
http.Dir("testdata/example"),
)
}