github.com/posener/gitfs@v1.2.2-0.20200410105819-ea4e48d73ab9/gitfs_test.go (about)

     1  package gitfs
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/posener/gitfs/fsutil"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  	"golang.org/x/oauth2"
    15  )
    16  
    17  // With gitfs you can open a remote git repository, and load any file,
    18  // including non-go files.
    19  // In this example, the README.md file of a remote repository is loaded.
    20  func Example_open() {
    21  	ctx := context.Background()
    22  
    23  	// The load path is of the form: github.com/<owner>/<repo>(/<path>)?(@<ref>)?.
    24  	// `ref` can reference any git tag or branch. If github releases are in Semver format,
    25  	// the `tags/` prefix is not needed in the `ref` part.
    26  	fs, err := New(ctx, "github.com/kelseyhightower/helloworld@3.0.0")
    27  	if err != nil {
    28  		log.Fatalf("Failed initialize filesystem: %s", err)
    29  	}
    30  
    31  	// Open any file in the github repository, using the `Open` function. Both files
    32  	// and directory can be opened. The content is not loaded until it is actually being
    33  	// read. The content is loaded only once.
    34  	f, err := fs.Open("README.md")
    35  	if err != nil {
    36  		log.Fatalf("Failed opening file: %s", err)
    37  	}
    38  
    39  	// Copy the content to stdout.
    40  	io.Copy(os.Stdout, f)
    41  
    42  	// Output: # helloworld
    43  }
    44  
    45  // The ./fsutil package is a collection of useful functions that can work with
    46  // any `http.FileSystem` implementation.
    47  // For example, here we will use a function that loads go templates from the
    48  // filesystem.
    49  func Example_fsutil() {
    50  	ctx := context.Background()
    51  
    52  	// Open a git remote repository `posener/gitfs` in path `examples/templates`.
    53  	fs, err := New(ctx, "github.com/posener/gitfs/examples/templates")
    54  	if err != nil {
    55  		log.Fatalf("Failed initialize filesystem: %s", err)
    56  	}
    57  
    58  	// Use util function that loads all templates according to a glob pattern.
    59  	tmpls, err := fsutil.TmplParseGlob(fs, nil, "*.gotmpl")
    60  	if err != nil {
    61  		log.Fatalf("Failed parsing templates: %s", err)
    62  	}
    63  
    64  	// Execute the template and write to stdout.
    65  	tmpls.ExecuteTemplate(os.Stdout, "tmpl1.gotmpl", "Foo")
    66  
    67  	// Output: Hello, Foo
    68  }
    69  
    70  // Tests not supported repository pattern.
    71  func TestNew_notSupported(t *testing.T) {
    72  	t.Parallel()
    73  	ctx := context.Background()
    74  	_, err := New(ctx, "git.com/nosuchusername/nosuchproject")
    75  	require.Error(t, err)
    76  }
    77  
    78  // Tests loading of local repository.
    79  func TestNew_local(t *testing.T) {
    80  	t.Parallel()
    81  	ctx := context.Background()
    82  	_, err := New(ctx, "github.com/posener/gitfs", OptLocal("."))
    83  	require.NoError(t, err)
    84  }
    85  
    86  func TestWithContext(t *testing.T) {
    87  	t.Parallel()
    88  	fs, err := New(context.Background(), "github.com/posener/gitfs")
    89  	require.NoError(t, err)
    90  	f, err := fs.Open("README.md")
    91  	require.NoError(t, err)
    92  	ctx, cancel := context.WithCancel(context.Background())
    93  	cancel()
    94  	f = WithContext(f, ctx)
    95  	_, err = f.Read(make([]byte, 10))
    96  	assert.EqualError(t, err, "failed getting blob: context canceled")
    97  }
    98  
    99  func init() {
   100  	// Set Github access token in default client if available
   101  	// from environment variables.
   102  	token := os.Getenv("GITHUB_TOKEN")
   103  	if token != "" {
   104  		http.DefaultClient = oauth2.NewClient(
   105  			context.Background(),
   106  			oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}))
   107  	}
   108  }