Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internal/js_scanner/js_scanner_test.go#FuzzHoistImport #675

Merged
merged 3 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init: pnpm install && pnpm run build && go get && go build ./... && go test ./... && make
command: go run .


81 changes: 52 additions & 29 deletions internal/js_scanner/js_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"strings"
"testing"
"unicode/utf8"

"github.com/withastro/compiler/internal/test_utils"
)
Expand All @@ -16,8 +17,8 @@ type testcase struct {
only bool
}

func TestHoistImport(t *testing.T) {
tests := []testcase{
func fixturesHoistImport() []testcase {
return []testcase{
{
name: "basic",
source: `const value = "test"`,
Expand All @@ -40,18 +41,18 @@ const article2 = await import('../markdown/article2.md')
{
name: "big import",
source: `import {
a,
b,
c,
d,
a,
b,
c,
d,
} from "package"

const b = await fetch();`,
want: `import {
a,
b,
c,
d,
a,
b,
c,
d,
} from "package"
`,
},
Expand All @@ -73,18 +74,18 @@ const b = await fetch();`,
name: "import assertion 2",
source: `// comment
import {
fn
fn
} from
"package" assert {
it: 'works'
};
"package" assert {
it: 'works'
};
const b = await fetch();`,
want: `import {
fn
fn
} from
"package" assert {
it: 'works'
};
"package" assert {
it: 'works'
};
`,
},
{
Expand All @@ -96,10 +97,10 @@ import Test from "../components/Test.astro";`,
{
name: "import.meta.env II",
source: `console.log(
import
.meta
.env
.FOO
import
.meta
.env
.FOO
);
import Test from "../components/Test.astro";`,
want: `import Test from "../components/Test.astro";`,
Expand All @@ -115,7 +116,7 @@ const b = await fetch()`,
name: "getStaticPaths",
source: `import { fn } from "package";
export async function getStaticPaths() {
const content = Astro.fetchContent('**/*.md');
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `import { fn } from "package";`,
Expand All @@ -124,7 +125,7 @@ const b = await fetch()`,
name: "getStaticPaths with comments",
source: `import { fn } from "package";
export async function getStaticPaths() {
const content = Astro.fetchContent('**/*.md');
const content = Astro.fetchContent('**/*.md');
}
const b = await fetch()`,
want: `import { fn } from "package";`,
Expand All @@ -133,29 +134,29 @@ const b = await fetch()`,
name: "getStaticPaths with semicolon",
source: `import { fn } from "package";
export async function getStaticPaths() {
const content = Astro.fetchContent('**/*.md');
const content = Astro.fetchContent('**/*.md');
}; const b = await fetch()`,
want: `import { fn } from "package";`,
},
{
name: "getStaticPaths with RegExp escape",
source: `export async function getStaticPaths() {
const pattern = /\.md$/g.test('value');
const pattern = /\.md$/g.test('value');
}
import a from "a";`,
want: `import a from "a";`,
},
{
name: "getStaticPaths with divider",
source: `export async function getStaticPaths() {
const pattern = a / b;
const pattern = a / b;
}`,
want: ``,
},
{
name: "getStaticPaths with divider and following content",
source: `export async function getStaticPaths() {
const value = 1 / 2;
const value = 1 / 2;
}
// comment
import { b } from "b";
Expand All @@ -165,7 +166,7 @@ const { a } = Astro.props;`,
{
name: "getStaticPaths with regex and following content",
source: `export async function getStaticPaths() {
const value = /2/g;
const value = /2/g;
}
// comment
import { b } from "b";
Expand Down Expand Up @@ -203,6 +204,10 @@ import { c } from "c";
`,
},
}
}

func TestHoistImport(t *testing.T) {
tests := fixturesHoistImport()
for _, tt := range tests {
if tt.only {
tests = make([]testcase, 0)
Expand All @@ -226,6 +231,24 @@ import { c } from "c";
}
}

func FuzzHoistImport(f *testing.F) {
tests := fixturesHoistImport()
for _, tt := range tests {
f.Add(tt.source) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, source string) {
result := HoistImports([]byte(source))
got := []byte{}
for _, imp := range result.Hoisted {
got = append(got, bytes.TrimSpace(imp)...)
got = append(got, '\n')
}
if utf8.ValidString(source) && !utf8.ValidString(string(got)) {
t.Errorf("HTML scoping produced invalid html string: %q", got)
jasikpark marked this conversation as resolved.
Show resolved Hide resolved
}
})
}

func TestHoistExport(t *testing.T) {
tests := []testcase{
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("import\"\nimport \"\";")