cue/load: factor out addFiles.

Change-Id: I38f451b3b675fefca1f785dff5321b22047413d1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4561
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/load/import.go b/cue/load/import.go
index 5a46fc4..4f21afa 100644
--- a/cue/load/import.go
+++ b/cue/load/import.go
@@ -185,16 +185,7 @@
 		return p
 	}
 
-	for _, f := range p.CUEFiles {
-		if !ctxt.isAbsPath(f) {
-			f = ctxt.joinPath(cfg.ModuleRoot, f)
-		}
-		r, err := ctxt.openFile(f)
-		if err != nil {
-			p.ReportError(err)
-		}
-		_ = p.AddFile(f, r)
-	}
+	l.addFiles(cfg.ModuleRoot, p)
 	p.Complete()
 	return p
 }
diff --git a/cue/load/loader.go b/cue/load/loader.go
index 8d6b349..debd4ae 100644
--- a/cue/load/loader.go
+++ b/cue/load/loader.go
@@ -152,17 +152,7 @@
 	// 	bp.ImportPath = ModDirImportPath(dir)
 	// }
 
-	for _, f := range pkg.CUEFiles {
-		fs := &l.cfg.fileSystem
-		if !fs.isAbsPath(f) {
-			f = fs.joinPath(cfg.Dir, f)
-		}
-		r, err := fs.openFile(f)
-		if err != nil {
-			pkg.ReportError(err)
-		}
-		_ = pkg.AddFile(f, r)
-	}
+	l.addFiles(cfg.Dir, pkg)
 
 	pkg.Local = true
 	l.stk.Push("user")
@@ -176,6 +166,23 @@
 	return pkg
 }
 
+func (l *loader) addFiles(dir string, p *build.Instance) {
+	files := p.CUEFiles
+	fs := &l.cfg.fileSystem
+
+	for _, f := range files {
+		if !fs.isAbsPath(f) {
+			f = fs.joinPath(dir, f)
+		}
+		r, err := fs.openFile(f)
+		if err != nil {
+			p.ReportError(err)
+		}
+
+		_ = p.AddFile(f, r)
+	}
+}
+
 func cleanImport(path string) string {
 	orig := path
 	path = pathpkg.Clean(path)
diff --git a/cue/load/loader_test.go b/cue/load/loader_test.go
index a7aa867..c6de0ab 100644
--- a/cue/load/loader_test.go
+++ b/cue/load/loader_test.go
@@ -241,9 +241,6 @@
 display:./toolonly`,
 	}}
 	for i, tc := range testCases {
-		// if i != 5 {
-		// 	continue
-		// }
 		t.Run(strconv.Itoa(i)+"/"+strings.Join(tc.args, ":"), func(t *testing.T) {
 			pkgs := Instances(tc.args, tc.cfg)
 
@@ -261,7 +258,7 @@
 			want := strings.TrimSpace(tc.want)
 			want = strings.Replace(want, "\t", "    ", -1)
 			if got != want {
-				t.Errorf("\n%s", diff.Diff(got, want))
+				t.Errorf("\n%s", diff.Diff(want, got))
 				t.Logf("\n%s", got)
 			}
 		})