blob: 3b7aada05d6cc7dad914bf184055b8ad8e8f8e8c [file]
// Copyright 2020 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 integrationtests
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"go.fuchsia.dev/jiri/project"
)
func TestProjectIgnoresByAttribute(t *testing.T) {
t.Parallel()
numProjects := 3
numOptionalProjects := 2
totalProjects := numProjects + numOptionalProjects
numManifestProjects := 1
manifestRemote := t.TempDir()
manifest := project.Manifest{
Projects: []project.Project{
{
Name: "manifest",
Path: "manifest",
Remote: manifestRemote,
},
},
}
createRemoteProj := func(i int, attributes string) {
t.Helper()
name := fmt.Sprintf("project-%d", i)
path := fmt.Sprintf("path-%d", i)
remoteDir := t.TempDir()
setupGitRepo(t, remoteDir, map[string]any{
"README": "initial readme",
})
manifest.Projects = append(manifest.Projects, project.Project{
Name: name,
Path: path,
Remote: remoteDir,
Attributes: attributes,
})
}
for i := range numProjects {
createRemoteProj(i, "")
}
for i := numProjects; i < totalProjects; i++ {
createRemoteProj(i, "optional")
}
setupGitRepo(t, manifestRemote, map[string]any{
"manifest": manifest,
})
root := t.TempDir()
jiri := jiriInit(t, root)
jiri("import", "manifest", manifestRemote)
jiri("update", "-gc")
outputPath := filepath.Join(t.TempDir(), "output.json")
jiri("project", "-json-output", outputPath, "-list-remote-projects")
var projectInfo []map[string]any
bytes, err := os.ReadFile(outputPath)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(bytes, &projectInfo); err != nil {
t.Fatal(err)
}
expectedProjects := numProjects + numManifestProjects
if len(projectInfo) != expectedProjects {
t.Errorf("Unexpected number of projects returned (%d, %d) (want, got)\n%v", expectedProjects, len(projectInfo), projectInfo)
}
jiri("init", "-fetch-optional=optional", root)
jiri("update", "-gc")
jiri("project", "-json-output", outputPath, "-list-remote-projects")
bytes, err = os.ReadFile(outputPath)
if err != nil {
t.Fatal(err)
}
projectInfo = nil
if err := json.Unmarshal(bytes, &projectInfo); err != nil {
t.Fatal(err)
}
expectedProjects = totalProjects + numManifestProjects
if len(projectInfo) != expectedProjects {
t.Errorf("Unexpected number of projects returned (%d, %d) (want, got)\n%v", expectedProjects, len(projectInfo), projectInfo)
}
}
func TestProjectListRemoteTopology(t *testing.T) {
t.Parallel()
petalRemote := t.TempDir()
setupGitRepo(t, petalRemote, map[string]any{"README": "petal"})
petalRev := currentRevision(t, petalRemote)
vendorRemote := t.TempDir()
setupGitRepo(t, vendorRemote, map[string]any{
"manifests/platform": project.Manifest{
LocalImports: []project.LocalImport{{File: "third_party"}},
Projects: []project.Project{
{Name: "vendor/foo", Path: "vendor/foo", Remote: vendorRemote},
},
},
"manifests/third_party": project.Manifest{
Projects: []project.Project{
{Name: "petal", Path: "petal", Remote: petalRemote, Revision: petalRev},
},
},
})
vendorRev := currentRevision(t, vendorRemote)
fuchsiaRemote := t.TempDir()
setupGitRepo(t, fuchsiaRemote, map[string]any{
"manifests/platform": project.Manifest{
Imports: []project.Import{
{
Name: "vendor/foo",
Manifest: "manifests/platform",
Remote: vendorRemote,
Revision: vendorRev,
},
},
Projects: []project.Project{
{Name: "fuchsia", Path: "fuchsia", Remote: fuchsiaRemote},
},
},
})
fuchsiaRev := currentRevision(t, fuchsiaRemote)
integrationRemote := t.TempDir()
setupGitRepo(t, integrationRemote, map[string]any{
"minimal": project.Manifest{
LocalImports: []project.LocalImport{
{File: "fuchsia/stem"},
},
Projects: []project.Project{
{Name: "integration", Path: "integration", Remote: integrationRemote},
},
},
"fuchsia/stem": project.Manifest{
Imports: []project.Import{
{
Name: "fuchsia",
Manifest: "manifests/platform",
Remote: fuchsiaRemote,
Revision: fuchsiaRev,
},
},
},
})
root := t.TempDir()
jiri := jiriInit(t, root)
jiri("import", "-name=integration", "minimal", integrationRemote)
jiri("update", "-gc")
outputPath := filepath.Join(t.TempDir(), "output.json")
jiri("project", "-list-remote-projects", "-json-output", outputPath)
raw, err := os.ReadFile(outputPath)
if err != nil {
t.Fatal(err)
}
type projectTopology struct {
Name string `json:"name"`
Manifest string `json:"manifest"`
ImportManifest string `json:"import_manifest"`
ImportedBy string `json:"imported_by"`
}
var got []projectTopology
if err := json.Unmarshal(raw, &got); err != nil {
t.Fatal(err)
}
byName := make(map[string]projectTopology, len(got))
for _, item := range got {
byName[item.Name] = item
}
cases := []struct {
name string
wantImportedBy string
wantManifest string
wantImportManifest string
}{
{
name: "integration",
wantImportedBy: "",
wantManifest: filepath.Join(root, "integration", "minimal"),
wantImportManifest: filepath.Join(root, ".jiri_manifest"),
},
{
name: "fuchsia",
wantImportedBy: "integration",
wantManifest: filepath.Join(root, "fuchsia", "manifests", "platform"),
wantImportManifest: filepath.Join(root, "integration", "fuchsia", "stem"),
},
{
name: "vendor/foo",
wantImportedBy: "fuchsia",
wantManifest: filepath.Join(root, "vendor", "foo", "manifests", "platform"),
wantImportManifest: filepath.Join(root, "fuchsia", "manifests", "platform"),
},
{
name: "petal",
wantImportedBy: "vendor/foo",
wantManifest: filepath.Join(root, "vendor", "foo", "manifests", "third_party"),
wantImportManifest: "",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
info, ok := byName[tc.name]
if !ok {
t.Fatalf("project %q missing from output: %+v", tc.name, got)
}
if info.ImportedBy != tc.wantImportedBy {
t.Errorf("imported_by = %q, want %q", info.ImportedBy, tc.wantImportedBy)
}
if info.Manifest != tc.wantManifest {
t.Errorf("manifest = %q, want %q", info.Manifest, tc.wantManifest)
}
if info.ImportManifest != tc.wantImportManifest {
t.Errorf("import_manifest = %q, want %q", info.ImportManifest, tc.wantImportManifest)
}
})
}
}