Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / mvdan.cc / xurls / v2@v2.2.0 / cmd / xurls / main_test.go
1 // Copyright (c) 2019, Daniel Martí <mvdan@mvdan.cc>
2 // See LICENSE for licensing information
3
4 package main
5
6 import (
7         "context"
8         "fmt"
9         "io/ioutil"
10         "net"
11         "net/http"
12         "os"
13         "path/filepath"
14         "testing"
15
16         "github.com/rogpeppe/go-internal/testscript"
17 )
18
19 func TestMain(m *testing.M) {
20         os.Exit(testscript.RunMain(m, map[string]func() int{
21                 "xurls": main1,
22         }))
23 }
24
25 func TestScripts(t *testing.T) {
26         t.Parallel()
27         testscript.Run(t, testscript.Params{
28                 Dir: filepath.Join("testdata", "scripts"),
29                 Setup: func(env *testscript.Env) error {
30                         mux := http.NewServeMux()
31                         mux.HandleFunc("/plain", func(w http.ResponseWriter, r *http.Request) {
32                                 fmt.Fprintf(w, "plaintext")
33                         })
34                         mux.HandleFunc("/redir-1", func(w http.ResponseWriter, r *http.Request) {
35                                 http.Redirect(w, r, "/plain", http.StatusMovedPermanently)
36                         })
37                         mux.HandleFunc("/redir-2", func(w http.ResponseWriter, r *http.Request) {
38                                 http.Redirect(w, r, "/redir-1", http.StatusMovedPermanently)
39                         })
40                         ln, err := net.Listen("tcp", ":0")
41                         if err != nil {
42                                 return err
43                         }
44                         server := &http.Server{Handler: mux}
45                         go server.Serve(ln)
46                         env.Vars = append(env.Vars, "SERVER=http://"+ln.Addr().String())
47                         env.Defer(func() {
48                                 if err := server.Shutdown(context.TODO()); err != nil {
49                                         t.Fatal(err)
50                                 }
51                         })
52                         return nil
53                 },
54                 Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
55                         "expand": func(ts *testscript.TestScript, neg bool, args []string) {
56                                 if neg {
57                                         ts.Fatalf("unsupported: ! expand")
58                                 }
59                                 if len(args) == 0 {
60                                         ts.Fatalf("usage: expand file...")
61                                 }
62                                 for _, arg := range args {
63                                         data := ts.ReadFile(arg)
64                                         data = os.Expand(data, ts.Getenv)
65                                         err := ioutil.WriteFile(ts.MkAbs(arg), []byte(data), 0666)
66                                         ts.Check(err)
67                                 }
68                         },
69                 },
70         })
71 }