You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ja...@apache.org on 2026/10/18 13:35:06 UTC

(mynewt-newt) 08/08: newt: Test that upgrade fetches new commits and versions

This is an automated email from the ASF dual-hosted git repository.

sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git

commit 69805b883e82cf9b7b8a4e4784d51006751c21a3
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Fri Oct 16 17:23:09 2026 +0200

    newt: Test that upgrade fetches new commits and versions
    
    All upgrade tests cloned repos fresh, so fetching an already installed
    repo was not tested; making GitDownloader.Fetch a no-op went
    unnoticed.
    
    TestUpgradeFetch installs a project, then pushes a new commit to the
    master branch repo 04 is installed at, and a new release tag to repo
    05, which the project then requires. Upgrade must pick up both.
    
    Assisted-by: Claude:claude-opus-5-5
---
 newt/testdata/upgrade/fetch/expected.txt |  2 +
 newt/testdata/upgrade/fetch/project.yml  | 31 +++++++++++
 newt/upgrade_test.go                     | 95 +++++++++++++++++++++++++++++++-
 3 files changed, 126 insertions(+), 2 deletions(-)

diff --git a/newt/testdata/upgrade/fetch/expected.txt b/newt/testdata/upgrade/fetch/expected.txt
new file mode 100644
index 00000000..f885a76e
--- /dev/null
+++ b/newt/testdata/upgrade/fetch/expected.txt
@@ -0,0 +1,2 @@
+    * mynewt-dummy-repo-04: 503782e265da53629ed26af485d01a3b7ac80cbd, 0.0.0
+    * mynewt-dummy-repo-05: 762d7da4ac4fc34df941b99fc0c57a030e5b1833, 2.0.0
diff --git a/newt/testdata/upgrade/fetch/project.yml b/newt/testdata/upgrade/fetch/project.yml
new file mode 100644
index 00000000..f071aded
--- /dev/null
+++ b/newt/testdata/upgrade/fetch/project.yml
@@ -0,0 +1,31 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+### TestUpgradeFetch adds commits to the repos and changes the repo-05
+### version after the initial upgrade.
+
+repository.mynewt-dummy-repo-04:
+   type: git
+   vers: 0.0.0
+   url: 'https://dummy.invalid/mynewt-dummy-repo-04.git'
+
+repository.mynewt-dummy-repo-05:
+   type: git
+   vers: 2.0.0
+   url: 'https://dummy.invalid/mynewt-dummy-repo-05.git'
diff --git a/newt/upgrade_test.go b/newt/upgrade_test.go
index 662dce1c..2c0ae409 100644
--- a/newt/upgrade_test.go
+++ b/newt/upgrade_test.go
@@ -33,16 +33,21 @@ const dummyRepoURL = "https://dummy.invalid/"
 
 var upgradeDir = filepath.Join("testdata", "upgrade")
 
-func runGit(t *testing.T, env []string, stdin *os.File, args ...string) {
+func runGit(t *testing.T, env []string, stdin *os.File,
+	args ...string) string {
+
 	t.Helper()
 
 	cmd := exec.Command("git", args...)
 	cmd.Env = append(os.Environ(), env...)
 	cmd.Stdin = stdin
 
-	if out, err := cmd.CombinedOutput(); err != nil {
+	out, err := cmd.CombinedOutput()
+	if err != nil {
 		t.Fatalf("git %s failed: %s\n%s", strings.Join(args, " "), err, out)
 	}
+
+	return strings.TrimSpace(string(out))
 }
 
 // dummyReposEnv imports dummy repos into a temporary directory and returns
@@ -188,3 +193,89 @@ func TestUpgradeAllowedIgnored(t *testing.T) {
 		readFile(t, filepath.Join(projDir, "expected.txt")),
 		strings.Join(installedRepos(t, projDir), "\n"))
 }
+
+// commitToDummyRepo clones the master branch of a dummy repo, lets edit
+// change files in the clone and pushes them as a new commit, optionally
+// tagged, back to the dummy repo. Returns the commit hash.
+func commitToDummyRepo(t *testing.T, env []string, repo string,
+	edit func(dir string), tag string) string {
+
+	t.Helper()
+
+	env = append(env,
+		"GIT_AUTHOR_NAME=newt test", "GIT_AUTHOR_EMAIL=test@dummy.invalid",
+		"GIT_COMMITTER_NAME=newt test",
+		"GIT_COMMITTER_EMAIL=test@dummy.invalid")
+
+	dir := filepath.Join(t.TempDir(), repo)
+	runGit(t, env, nil, "clone", "-q", dummyRepoURL+repo+".git", dir)
+
+	edit(dir)
+
+	runGit(t, env, nil, "-C", dir, "commit", "-q", "-a", "-m", "test commit")
+	runGit(t, env, nil, "-C", dir, "push", "-q", "origin", "master")
+	if tag != "" {
+		runGit(t, env, nil, "-C", dir, "tag", "-a", "-m", tag, tag)
+		runGit(t, env, nil, "-C", dir, "push", "-q", "origin", tag)
+	}
+
+	return runGit(t, env, nil, "-C", dir, "rev-parse", "HEAD")
+}
+
+// replaceInFile replaces the first occurrence of old in a file with new.
+func replaceInFile(t *testing.T, path string, old string, new string) {
+	t.Helper()
+
+	data := readFile(t, path)
+	if !strings.Contains(data, old) {
+		t.Fatalf("%s does not contain %q", path, old)
+	}
+
+	data = strings.Replace(data, old, new, 1)
+	if err := os.WriteFile(path, []byte(data), 0644); err != nil {
+		t.Fatal(err)
+	}
+}
+
+// Upgrade must fetch commits and versions added to repos after they were
+// installed.
+func TestUpgradeFetch(t *testing.T) {
+	env := dummyReposEnv(t)
+	projDir := copyToTemp(t, filepath.Join(upgradeDir, "fetch"))
+
+	newtOutput(t, projDir, env, "upgrade")
+	checkDummyRepoVersions(t, projDir, env)
+
+	// repo-04 is installed at its master branch, which gets a new commit.
+	commit04 := commitToDummyRepo(t, env, "mynewt-dummy-repo-04",
+		func(dir string) {
+			replaceInFile(t, filepath.Join(dir, "README.md"),
+				"# mynewt-dummy-repo-04", "# mynewt-dummy-repo-04 (updated)")
+		}, "")
+
+	// repo-05 gets a new release, which the project then requires.
+	commit05 := commitToDummyRepo(t, env, "mynewt-dummy-repo-05",
+		func(dir string) {
+			replaceInFile(t, filepath.Join(dir, "repository.yml"),
+				`"2.0.0": "mynewt_2_0_0_tag"`,
+				`"2.0.0": "mynewt_2_0_0_tag"`+"\n"+
+					`    "3.0.0": "mynewt_3_0_0_tag"`)
+		}, "mynewt_3_0_0_tag")
+	replaceInFile(t, filepath.Join(projDir, "project.yml"), "vers: 2.0.0",
+		"vers: 3.0.0")
+
+	newtOutput(t, projDir, env, "upgrade")
+
+	lines := []string{}
+	for _, line := range strings.Split(newtOutput(t, projDir, env, "info"),
+		"\n") {
+
+		if strings.Contains(line, "mynewt-dummy") {
+			lines = append(lines, line)
+		}
+	}
+	checkOutput(t, "repo versions",
+		fmt.Sprintf("* mynewt-dummy-repo-04: %s, 0.0.0\n"+
+			"* mynewt-dummy-repo-05: %s, 3.0.0", commit04, commit05),
+		strings.Join(lines, "\n"))
+}