| // Copyright 2021 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package codifier |
| |
| import ( |
| "strings" |
| "testing" |
| ) |
| |
| func TestAddTestsGroup(t *testing.T) { |
| var p *Proc |
| if p.AddTestsGroup("bar") != nil { |
| t.Error("expected nil Proc") |
| } |
| p = NewProcFromString("foo").setFilename("foofile").AddTestsGroup("bar") |
| if p == nil { |
| t.Error("expected non-nil Proc") |
| } |
| // No group("tests"), so confirm that it's added. |
| if !strings.HasPrefix(p.replacement, "foo\n\ngroup(\"tests\")") { |
| t.Errorf("didn't add test clause: %q", p.replacement) |
| } |
| if !strings.Contains(p.replacement, "deps = [ \"bar\" ]") { |
| t.Errorf("didn't add test: %q", p.replacement) |
| } |
| if len(p.changedFiles) != 1 && p.changedFiles[0] != "foolfile" { |
| t.Errorf("didn't add changedFiles: %q", p.changedFiles) |
| } |
| } |
| func TestAddTestsGroupWithGroupTests(t *testing.T) { |
| p := NewProcFromString("foo group(\"tests\"){ baz=bim } bar").setFilename("foofile") |
| if p.AddTestsGroup("adep") != nil { |
| t.Error("expected nil Proc because no deps") |
| } |
| s := "foo group(\"tests\"){ deps = [ \"adep\" ] } bar" |
| p = NewProcFromString(s).setFilename("foofile").AddTestsGroup("adep") |
| if p == nil { |
| t.Error("expected non-nil Proc") |
| } |
| // TODO: This test should confirm that p.replacement == s. But the change at |
| // orderedStrings broke the already-present check. Fix this test when the code |
| // is corrected. |
| if !strings.Contains(p.replacement, "deps = [ \"adep\",\n \"adep\"") { |
| t.Errorf("didn't add test: %q", p.replacement) |
| } |
| } |
| |
| func TestStopDirs(t *testing.T) { |
| // Passing "" as the Fuchsia directory may not generate correct |
| // directories because it attempt to use the cwd as the Fuchsia |
| // directory. |
| dirs, err := stopDirs("") |
| if err != nil { |
| t.Errorf("unexpected err: %v", err) |
| } |
| if len(dirs) != 3 { |
| t.Errorf("got %d dirs, expected 3", len(dirs)) |
| } |
| |
| dirs, err = stopDirs("/") |
| if err != nil { |
| t.Errorf("unexpected err: %v", err) |
| } |
| if len(dirs) != 3 { |
| t.Errorf("got %d dirs, expected 3", len(dirs)) |
| } |
| |
| dirs, err = stopDirs("foo/") |
| if err != nil { |
| t.Errorf("unexpected err: %v", err) |
| } |
| if len(dirs) != 3 { |
| t.Errorf("got %d dirs, expected 3", len(dirs)) |
| } |
| if !strings.HasSuffix(dirs[0], "foo") || |
| !strings.HasSuffix(dirs[1], "foo/zircon/system") || |
| !strings.HasSuffix(dirs[2], "foo/zircon/third_party") { |
| t.Errorf("unexpected stopDirs: %v", dirs) |
| } |
| } |
| |
| func TestAddParentTests(t *testing.T) { |
| var p *Proc |
| if p.AddParentTests("bar") != nil { |
| t.Error("expected nil Proc") |
| } |
| p = NewProcFromString("foo").AddParentTests("bar") |
| if p == nil { |
| t.Error("expected non-nil Proc") |
| } |
| if len(p.changedFiles) != 0 { |
| t.Error("expected no changedFiles") |
| } |
| } |