-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Printing * Added string methods to AST nodes Two new string methods have been added to the LiteralFileName and Ident nodes in the Abstract Syntax Tree (AST). These methods return the literal identifier of each node. Corresponding tests have also been added to ensure these new methods work as expected. * Added printing functionality for recipes and rules The print.go file has been updated to include the ability to print 'Recipe' and 'Rule' nodes. This includes writing the token, text, targets, prerequisites, and any associated recipes. Corresponding tests have also been added in print_test.go to ensure that these new functionalities work as expected. * Added error handling tests to print_test.go Enhanced the test suite in print_test.go by adding a DescribeTable for surfacing errors. This includes multiple entries that simulate different error positions within the writing process of targets, prerequisites, and recipes. The new tests ensure that our package correctly handles and reports these errors. * Updated README and codecov settings Added a new section in the README file explaining how to use `make.Fprint` for writing ast nodes. Also, adjusted the default target and threshold values under 'patch' in the codecov configuration.
- Loading branch information
1 parent
e2fa3ed
commit c8f7490
Showing
6 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ coverage: | |
default: | ||
target: 75% | ||
threshold: 3% | ||
patch: | ||
patch: | ||
default: | ||
target: 60% | ||
threshold: 10% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package make | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/unmango/go-make/ast" | ||
) | ||
|
||
func Fprint(w io.Writer, node ast.Node) (err error) { | ||
switch node := node.(type) { | ||
case *ast.LiteralFileName: | ||
_, err = io.WriteString(w, node.String()) | ||
case *ast.TargetList: | ||
for _, t := range node.List { | ||
if err = Fprint(w, t); err != nil { | ||
return | ||
} | ||
} | ||
_, err = io.WriteString(w, ":") | ||
case *ast.PreReqList: | ||
for _, t := range node.List { | ||
if err = Fprint(w, t); err != nil { | ||
return | ||
} | ||
} | ||
case *ast.Recipe: | ||
if _, err = fmt.Fprint(w, node.Tok); err != nil { | ||
return err | ||
} | ||
if _, err = io.WriteString(w, node.Text); err != nil { | ||
return err | ||
} | ||
if _, err = fmt.Fprintln(w); err != nil { | ||
return err | ||
} | ||
case *ast.Rule: | ||
if err = Fprint(w, node.Targets); err != nil { | ||
return err | ||
} | ||
if _, err = fmt.Fprint(w, " "); err != nil { | ||
return err | ||
} | ||
if err = Fprint(w, node.PreReqs); err != nil { | ||
return err | ||
} | ||
if _, err = fmt.Fprintln(w); err != nil { | ||
return err | ||
} | ||
for _, r := range node.Recipes { | ||
if err = Fprint(w, r); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package make_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/unmango/go-make" | ||
"github.com/unmango/go-make/ast" | ||
"github.com/unmango/go-make/internal/testing" | ||
"github.com/unmango/go-make/token" | ||
) | ||
|
||
var _ = Describe("Print", func() { | ||
Describe("Fprint", func() { | ||
It("should print a literal file name", func() { | ||
buf := &bytes.Buffer{} | ||
l := &ast.LiteralFileName{Name: &ast.Ident{ | ||
Name: "target", | ||
}} | ||
|
||
err := make.Fprint(buf, l) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buf.String()).To(Equal("target")) | ||
}) | ||
|
||
It("should print a target list", func() { | ||
buf := &bytes.Buffer{} | ||
t := &ast.TargetList{List: []ast.FileName{ | ||
&ast.LiteralFileName{Name: &ast.Ident{ | ||
Name: "target", | ||
}}, | ||
}} | ||
|
||
err := make.Fprint(buf, t) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buf.String()).To(Equal("target:")) | ||
}) | ||
|
||
It("should print a prereq list", func() { | ||
buf := &bytes.Buffer{} | ||
t := &ast.PreReqList{List: []ast.FileName{ | ||
&ast.LiteralFileName{Name: &ast.Ident{ | ||
Name: "prereq", | ||
}}, | ||
}} | ||
|
||
err := make.Fprint(buf, t) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buf.String()).To(Equal("prereq")) | ||
}) | ||
|
||
It("should print a recipe", func() { | ||
buf := &bytes.Buffer{} | ||
r := &ast.Recipe{ | ||
Tok: token.TAB, | ||
Text: "recipe", | ||
} | ||
|
||
err := make.Fprint(buf, r) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buf.String()).To(Equal("\trecipe\n")) | ||
}) | ||
|
||
It("should print a rule", func() { | ||
buf := &bytes.Buffer{} | ||
r := &ast.Rule{ | ||
Targets: &ast.TargetList{List: []ast.FileName{ | ||
&ast.LiteralFileName{Name: &ast.Ident{ | ||
Name: "target", | ||
}}, | ||
}}, | ||
PreReqs: &ast.PreReqList{List: []ast.FileName{ | ||
&ast.LiteralFileName{Name: &ast.Ident{ | ||
Name: "prereq", | ||
}}, | ||
}}, | ||
Recipes: []*ast.Recipe{{ | ||
Tok: token.TAB, | ||
Text: "recipe", | ||
}}, | ||
} | ||
|
||
err := make.Fprint(buf, r) | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buf.String()).To(Equal("target: prereq\n\trecipe\n")) | ||
}) | ||
|
||
DescribeTable("should surface errors", | ||
Entry("write target", 1), | ||
Entry("write colon", 2), | ||
Entry("write space", 3), | ||
Entry("write prereq", 4), | ||
Entry("write newline", 5), | ||
Entry("write tab", 6), | ||
Entry("write recipe", 7), | ||
Entry("write newline", 8), | ||
func(position int) { | ||
w := testing.NewErrAfterWriter(position) | ||
r := &ast.Rule{ | ||
Targets: &ast.TargetList{List: []ast.FileName{ | ||
&ast.LiteralFileName{Name: &ast.Ident{ | ||
Name: "target", | ||
}}, | ||
}}, | ||
PreReqs: &ast.PreReqList{List: []ast.FileName{ | ||
&ast.LiteralFileName{Name: &ast.Ident{ | ||
Name: "prereq", | ||
}}, | ||
}}, | ||
Recipes: []*ast.Recipe{{ | ||
Tok: token.TAB, | ||
Text: "recipe", | ||
}}, | ||
} | ||
|
||
err := make.Fprint(w, r) | ||
|
||
Expect(err).To(MatchError(fmt.Sprintf("write err: %d", position))) | ||
}, | ||
) | ||
}) | ||
}) |