Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/hudson/plugins/mercurial/MercurialSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public enum RevisionType {
@Deprecated
private String branch;

private String headName = "default";

/** Slash-separated subdirectory of the workspace in which the repository will be kept; null for top level. */
private String subdir;

Expand Down Expand Up @@ -300,6 +302,14 @@ public boolean isDisableChangeLog() {
this.revision = Util.fixEmpty(revision) == null ? "default" : revision;
}

public @NonNull String getHeadName() {
return headName;
}

public final void setHeadName(@NonNull String headName) {
this.headName = Util.fixEmpty(headName) == null ? "default" : headName;
}

@Deprecated
public String getBranch() {
if (revisionType != RevisionType.BRANCH) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ B withSource(String source) {
MercurialSCM result = new MercurialSCM(source());
if (revision instanceof MercurialSCMSource.MercurialRevision) {
result.setRevisionType(MercurialSCM.RevisionType.CHANGESET);
result.setRevision(((MercurialSCMSource.MercurialRevision) revision).getHash());
MercurialSCMSource.MercurialRevision mercurialRevision = (MercurialSCMSource.MercurialRevision) revision;
result.setRevision(mercurialRevision.getHash());
result.setHeadName(mercurialRevision.getHead().getName());
} else {
result.setRevisionType(MercurialSCM.RevisionType.BRANCH);
result.setRevision(head().getName());
result.setHeadName(head().getName());
}
result.setBrowser(browser());
result.setClean(clean());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package hudson.plugins.mercurial;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.FilePath;
import hudson.tools.ToolProperty;
import hudson.util.LogTaskListener;
import hudson.util.StreamTaskListener;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMRevision;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.assertEquals;

public class MercurialSCMBuilderTest {
@Rule
public JenkinsRule j = new JenkinsRule();
@Rule
public MercurialRule m = new MercurialRule(j);
@Rule
public TemporaryFolder tmp = new TemporaryFolder();

private static LogTaskListener listener;
private static MercurialSCMSource mercurialSCMSource;

@Before
public void prepareEnvironment() throws Exception {
String instName = "caching";
MercurialInstallation installation = new MercurialInstallation(instName, "", "hg", false, true, null, false, null,
Collections.<ToolProperty<?>>emptyList());
listener = new LogTaskListener(Logger.getLogger(MercurialSCMSourceTest.class.getName()), Level.INFO);
j.jenkins.getDescriptorByType(MercurialInstallation.DescriptorImpl.class).setInstallations(installation);
FilePath repo = new FilePath(tmp.getRoot());
installation.forNode(j.jenkins, StreamTaskListener.fromStdout());
m.hg(repo, "init");
repo.child("file").write("initial content", "UTF-8");
m.hg(repo, "commit", "--addremove", "--message=initial");
m.hg(repo, "tag", "version-1.0");
m.hg(repo, "branch", "my-branch");
repo.child("file2").write("content in branch", "UTF-8");
m.hg(repo, "commit", "--addremove", "--message=branch");
m.hg(repo, "tag", "version-1.1");

installation.forNode(j.jenkins, StreamTaskListener.fromStdout());
mercurialSCMSource = new MercurialSCMSource(null, instName, tmp.getRoot().toURI().toURL().toString(), null, null, null, null, null, true);
}

@Test
public void headNameEquals() throws IOException, InterruptedException {
Map<SCMHead, SCMRevision> result = mercurialSCMSource.fetch(null, SCMHeadObserver.collect(), null, null).result();
for (Map.Entry<SCMHead, SCMRevision> entry : result.entrySet()) {
MercurialSCM mercurialSCM = new MercurialSCMBuilder(entry.getKey(), entry.getValue(), "", "")
.build();
assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType());
assertEquals(entry.getValue().getHead().getName(), mercurialSCM.getHeadName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is just asserting that a newly introduced method has an expected value. Fine so far as it goes, but does not demonstrate that the purported purpose of the change (to fix some behavior of resolveScm) is actually accomplished by doing that. Is some piece of code using reflection to look for a JavaBeans property named headName? (I certainly hope not!)

Copy link
Author

@long76 long76 Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reflection here of course) just check all existing heads) resolveScm works correct just it return MercurialSCM where you can get only hash of commit. in fact i dont know which test need for it but my tests confirm that headName correct. maybe need other name of variable) but it's not breaking change like change value of revision from hash to branch name

assertEquals(entry.getKey().getName(), mercurialSCM.getHeadName());
}
}

@Test
public void headNameDefault() throws IOException, InterruptedException {
SCMRevision revision = mercurialSCMSource.fetch("version-1.0", listener, null);
MercurialSCM mercurialSCM = new MercurialSCMBuilder(revision.getHead(), revision, "", "")
.build();
assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType());
assertEquals("default", mercurialSCM.getHeadName());
assertEquals(revision.getHead().getName(), mercurialSCM.getHeadName());
}

@Test
public void headNameNonDefault() throws IOException, InterruptedException {
SCMRevision revision = mercurialSCMSource.fetch("version-1.1", listener, null);
MercurialSCM mercurialSCM = new MercurialSCMBuilder(revision.getHead(), revision, "", "")
.build();
assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType());
assertEquals("my-branch", mercurialSCM.getHeadName());
assertEquals(revision.getHead().getName(), mercurialSCM.getHeadName());
}
}