package org.leguan.release;

import org.gradle.api.Task;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Assert;
import org.junit.Test;

public class ReleasePluginTest {

    @Test
    public void tasks () {
        ProjectInternal project = (ProjectInternal) ProjectBuilder.builder().build();

        project.getPlugins().apply(ReleasePlugin.class);
        ReleaseDsl releaseDsl = (ReleaseDsl) project.getExtensions().findByName(ReleasePlugin.RELEASE_EXTENSION);
        releaseDsl.setReleaseTaskName("releaseSnapshot");
        project.evaluate();

        Task releaseSnapshotTask = project.task("releaseSnapshot");
        CollectVersionTask collectVersionTask = (CollectVersionTask) project.getTasks().findByName(ReleasePlugin.COLLECT_VERSIONS_TASKNAME);
        Assert.assertTrue (collectVersionTask.getDependsOn().contains(releaseSnapshotTask));
        Assert.assertNotNull(project.getTasks().findByName(ReleasePlugin.INJECT_VERSIONS_TASKNAME));
    }

    @Test
    public void releaseSnapshotInSubmodule () {
        ProjectInternal rootproject = (ProjectInternal) ProjectBuilder.builder().withName("rootproject").build();
        ProjectInternal subproject = (ProjectInternal) ProjectBuilder.builder().withParent(rootproject).withName("subproject").build();

        rootproject.getPlugins().apply(ReleasePlugin.class);
        ReleaseDsl releaseDsl = (ReleaseDsl) rootproject.getExtensions().findByName(ReleasePlugin.RELEASE_EXTENSION);
        releaseDsl.setReleaseTaskName("releaseSnapshot");

        rootproject.evaluate();

        Task releaseSnapshotTask = subproject.task("releaseSnapshot");

        CollectVersionTask collectVersionTask = (CollectVersionTask) rootproject.getTasks().findByName(ReleasePlugin.COLLECT_VERSIONS_TASKNAME);
        Assert.assertTrue (collectVersionTask.getDependsOn().contains(releaseSnapshotTask));
    }
}
