| // Copyright 2025 The Dawn & Tint Authors |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are met: |
| // |
| // 1. Redistributions of source code must retain the above copyright notice, this |
| // list of conditions and the following disclaimer. |
| // |
| // 2. Redistributions in binary form must reproduce the above copyright notice, |
| // this list of conditions and the following disclaimer in the documentation |
| // and/or other materials provided with the distribution. |
| // |
| // 3. Neither the name of the copyright holder nor the names of its |
| // contributors may be used to endorse or promote products derived from |
| // this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| package main |
| |
| import ( |
| "testing" |
| |
| "dawn.googlesource.com/dawn/tools/src/fileutils" |
| "dawn.googlesource.com/dawn/tools/src/oswrapper" |
| "github.com/stretchr/testify/require" |
| ) |
| |
| func TestGatherWgslFiles(t *testing.T) { |
| tests := []struct { |
| name string |
| inputsDir string |
| outDir string |
| setupFS func(t *testing.T, fs oswrapper.FSTestOSWrapper) |
| wantErr bool |
| verify func(t *testing.T, fs oswrapper.FSTestOSWrapper) |
| }{ |
| { |
| name: "Basic copy", |
| inputsDir: "/in", |
| outDir: "/out", |
| setupFS: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| require.NoError(t, fs.MkdirAll("/in/subdir", 0777)) |
| require.NoError(t, fs.MkdirAll("/out", 0777)) |
| require.NoError(t, fs.WriteFile("/in/a.wgsl", []byte("shader a"), 0666)) |
| require.NoError(t, fs.WriteFile("/in/b.txt", []byte("not a shader"), 0666)) |
| require.NoError(t, fs.WriteFile("/in/c.expected.wgsl", []byte("should be ignored"), 0666)) |
| require.NoError(t, fs.WriteFile("/in/subdir/d.wgsl", []byte("shader d"), 0666)) |
| }, |
| wantErr: false, |
| verify: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| // Check that expected files were created with correct content |
| contentA, err := fs.ReadFile("/out/a.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "shader a", string(contentA)) |
| |
| contentD, err := fs.ReadFile("/out/subdir_d.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "shader d", string(contentD)) |
| |
| // Check that other files were not copied |
| _, err = fs.Stat("/out/b.txt") |
| require.Error(t, err, "b.txt should not have been copied") |
| |
| _, err = fs.Stat("/out/c.expected.wgsl") |
| require.Error(t, err, "c.expected.wgsl should not have been copied") |
| }, |
| }, |
| { |
| name: "Complex subdirectories", |
| inputsDir: "/in", |
| outDir: "/out", |
| setupFS: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| require.NoError(t, fs.MkdirAll("/in/a/b", 0777)) |
| require.NoError(t, fs.MkdirAll("/out", 0777)) |
| require.NoError(t, fs.WriteFile("/in/a/b/c.wgsl", []byte("shader c"), 0666)) |
| }, |
| wantErr: false, |
| verify: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| content, err := fs.ReadFile("/out/a_b_c.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "shader c", string(content)) |
| }, |
| }, |
| { |
| name: "Empty input directory", |
| inputsDir: "/in", |
| outDir: "/out", |
| setupFS: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| require.NoError(t, fs.MkdirAll("/in", 0777)) |
| require.NoError(t, fs.MkdirAll("/out", 0777)) |
| }, |
| wantErr: false, |
| verify: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| isEmpty, err := fileutils.IsEmptyDir("/out", fs) |
| require.NoError(t, err) |
| require.True(t, isEmpty) |
| }, |
| }, |
| { |
| name: "Output directory does not exist", |
| inputsDir: "/in", |
| outDir: "/out", |
| setupFS: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| require.NoError(t, fs.MkdirAll("/in", 0777)) |
| require.NoError(t, fs.WriteFile("/in/a.wgsl", []byte("shader a"), 0666)) |
| }, |
| wantErr: false, |
| verify: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| // Check that output directory was created |
| info, err := fs.Stat("/out") |
| require.NoError(t, err) |
| require.True(t, info.IsDir()) |
| |
| // Check that file was copied |
| content, err := fs.ReadFile("/out/a.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "shader a", string(content)) |
| }, |
| }, |
| { |
| name: "Input directory does not exist", |
| inputsDir: "/nonexistent", |
| outDir: "/out", |
| setupFS: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| require.NoError(t, fs.MkdirAll("/out", 0777)) |
| }, |
| wantErr: true, |
| }, |
| { |
| name: "Adaptive comment stripping", |
| inputsDir: "/in", |
| outDir: "/out", |
| setupFS: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| require.NoError(t, fs.MkdirAll("/in", 0777)) |
| require.NoError(t, fs.MkdirAll("/out", 0777)) |
| |
| // Case 1: No comments |
| require.NoError(t, fs.WriteFile("/in/case1.wgsl", []byte("fn main() {}"), 0666)) |
| |
| // Case 2: Comments but no copyright |
| require.NoError(t, fs.WriteFile("/in/case2.wgsl", []byte("// This is a comment\nfn main() {}"), 0666)) |
| |
| // Case 3: Copyright but no other comments |
| copyrightNoComments := []byte("// Copyright 2020 The Dawn & Tint Authors\n//\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nfn main() {}") |
| require.NoError(t, fs.WriteFile("/in/case3.wgsl", copyrightNoComments, 0666)) |
| |
| // Case 4: Copyright and other comments |
| copyrightAndComments := []byte("// Copyright 2020 The Dawn & Tint Authors\n//\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n// This is a comment\nfn main() {}") |
| require.NoError(t, fs.WriteFile("/in/case4.wgsl", copyrightAndComments, 0666)) |
| }, |
| wantErr: false, |
| verify: func(t *testing.T, fs oswrapper.FSTestOSWrapper) { |
| // Case 1 validation: Just copied as-is |
| content1, err := fs.ReadFile("/out/case1.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "fn main() {}", string(content1)) |
| _, err = fs.Stat("/out/case1_no_comments.wgsl") |
| require.Error(t, err) |
| |
| // Case 2 validation: Copied as-is, and a copy without comments |
| content2, err := fs.ReadFile("/out/case2.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "// This is a comment\nfn main() {}", string(content2)) |
| |
| content2NoComments, err := fs.ReadFile("/out/case2_no_comments.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "\nfn main() {}", string(content2NoComments)) |
| |
| // Case 3 validation: Only one copy with copyright removed |
| content3, err := fs.ReadFile("/out/case3.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "fn main() {}", string(content3)) |
| _, err = fs.Stat("/out/case3_no_comments.wgsl") |
| require.Error(t, err) |
| |
| // Case 4 validation: Two copies, one with copyright notice removed and one with notice and all other comments removed |
| content4, err := fs.ReadFile("/out/case4.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "// This is a comment\nfn main() {}", string(content4)) |
| |
| content4NoComments, err := fs.ReadFile("/out/case4_no_comments.wgsl") |
| require.NoError(t, err) |
| require.Equal(t, "\nfn main() {}", string(content4NoComments)) |
| }, |
| }, |
| } |
| |
| for _, tc := range tests { |
| t.Run(tc.name, func(t *testing.T) { |
| wrapper := oswrapper.CreateFSTestOSWrapper() |
| if tc.setupFS != nil { |
| tc.setupFS(t, wrapper) |
| } |
| |
| err := gatherWgslFiles(tc.inputsDir, tc.outDir, wrapper) |
| |
| if tc.wantErr { |
| require.Error(t, err) |
| } else { |
| require.NoError(t, err) |
| } |
| |
| if tc.verify != nil { |
| tc.verify(t, wrapper) |
| } |
| }) |
| } |
| } |
| |
| func TestCheckInputFileType(t *testing.T) { |
| tests := []struct { |
| name string |
| content []byte |
| mode FuzzMode |
| wantErr bool |
| errMatch string |
| }{ |
| { |
| name: "Valid WGSL text file", |
| content: []byte("@fragment\nfn main() {}\n"), |
| mode: FuzzModeWgsl, |
| wantErr: false, |
| }, |
| { |
| name: "Invalid WGSL - binary file with null byte", |
| content: []byte{0x00, 0x01, 0x02, 0x03}, |
| mode: FuzzModeWgsl, |
| wantErr: true, |
| errMatch: "wrong file type detected: expected a text file for WGSL mode, but '/test' appears to be binary. Did you forget to add the -ir flag?", |
| }, |
| { |
| name: "Valid IR binary file (contains null byte)", |
| content: []byte{0x0a, 0x08, 0x00, 0x12, 0x04, 0x01, 0x02, 0x03}, |
| mode: FuzzModeIr, |
| wantErr: false, |
| }, |
| { |
| name: "Invalid IR - text file", |
| content: []byte("@fragment\nfn main() {}\n"), |
| mode: FuzzModeIr, |
| wantErr: true, |
| errMatch: "wrong file type detected: expected a binary file for IR mode, but '/test' appears to be text. Did you mean to remove the -ir flag?", |
| }, |
| { |
| name: "Empty file in WGSL mode (Success)", |
| content: []byte{}, |
| mode: FuzzModeWgsl, |
| wantErr: false, |
| }, |
| { |
| name: "Empty file in IR mode (Success)", |
| content: []byte{}, |
| mode: FuzzModeIr, |
| wantErr: false, |
| }, |
| } |
| |
| for _, tc := range tests { |
| t.Run(tc.name, func(t *testing.T) { |
| fs := oswrapper.CreateFSTestOSWrapper() |
| err := fs.WriteFile("/test", tc.content, 0666) |
| require.NoError(t, err) |
| |
| err = checkInputFileType("/test", tc.mode, fs) |
| if tc.wantErr { |
| require.Error(t, err) |
| require.Contains(t, err.Error(), tc.errMatch) |
| } else { |
| require.NoError(t, err) |
| } |
| }) |
| } |
| } |
| |
| func TestTaskModeAndFuzzModeString(t *testing.T) { |
| taskTests := []struct { |
| mode TaskMode |
| expected string |
| }{ |
| {TaskModeRun, "run"}, |
| {TaskModeCheck, "check"}, |
| {TaskModeGenerate, "generate"}, |
| {TaskModeTriage, "triage"}, |
| {TaskModeBisect, "bisect"}, |
| {TaskModeBisectStep, "bisect-step"}, |
| {TaskModeExperiment, "experiment"}, |
| {TaskMode(-1), "<unknown> (-1)"}, |
| {TaskMode(99), "<unknown> (99)"}, |
| } |
| |
| for _, tc := range taskTests { |
| require.Equal(t, tc.expected, tc.mode.String()) |
| } |
| |
| fuzzTests := []struct { |
| mode FuzzMode |
| expected string |
| }{ |
| {FuzzModeWgsl, "wgsl"}, |
| {FuzzModeIr, "ir"}, |
| {FuzzMode(-1), "<unknown> (-1)"}, |
| {FuzzMode(99), "<unknown> (99)"}, |
| } |
| |
| for _, tc := range fuzzTests { |
| require.Equal(t, tc.expected, tc.mode.String()) |
| } |
| } |