mirror of
https://github.com/onyx-and-iris/gignore.git
synced 2025-05-17 09:40:32 +01:00
add optional function WithFileWriter
This commit is contained in:
parent
891057ff86
commit
7df58273f5
@ -31,12 +31,12 @@ func (m mockFileWriter) Write(content []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateSuccess(t *testing.T) {
|
func TestCreateSuccess(t *testing.T) {
|
||||||
|
var templateBuffer bytes.Buffer
|
||||||
client := gignore.New(
|
client := gignore.New(
|
||||||
gignore.WithTemplateDirectory(gignore.DefaultTemplateDirectory),
|
gignore.WithTemplateDirectory(gignore.DefaultTemplateDirectory),
|
||||||
|
gignore.WithFileWriter(mockFileWriter{out: &templateBuffer}),
|
||||||
)
|
)
|
||||||
ctx := context.WithValue(context.Background(), clientKey, client)
|
ctx := context.WithValue(context.Background(), clientKey, client)
|
||||||
var templateBuffer bytes.Buffer
|
|
||||||
client.Writer = mockFileWriter{out: &templateBuffer}
|
|
||||||
|
|
||||||
var outBuffer bytes.Buffer
|
var outBuffer bytes.Buffer
|
||||||
err := createTemplate(ctx, &outBuffer, "go")
|
err := createTemplate(ctx, &outBuffer, "go")
|
||||||
|
@ -16,7 +16,7 @@ const DefaultTemplateDirectory = "gitignoreio"
|
|||||||
// Client is a client for managing .gitignore templates.
|
// Client is a client for managing .gitignore templates.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
registry *registry.TemplateRegistry
|
registry *registry.TemplateRegistry
|
||||||
Writer io.Writer
|
writer io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Client with the provided options.
|
// New creates a new Client with the provided options.
|
||||||
@ -85,7 +85,7 @@ func (c *Client) Create(template string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = c.Writer.Write(content)
|
_, err = c.writer.Write(content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package gignore
|
package gignore
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
// Option is a function that configures a GignoreClient.
|
// Option is a function that configures a GignoreClient.
|
||||||
type Option func(*Client)
|
type Option func(*Client)
|
||||||
|
|
||||||
@ -9,3 +11,10 @@ func WithTemplateDirectory(directory string) Option {
|
|||||||
c.registry.Directory = directory
|
c.registry.Directory = directory
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithFileWriter sets the file writer for the GignoreClient.
|
||||||
|
func WithFileWriter(writer io.Writer) Option {
|
||||||
|
return func(c *Client) {
|
||||||
|
c.writer = writer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user