Skip to content

Commit

Permalink
Add tests for root project name generation
Browse files Browse the repository at this point in the history
- part of #15655

Signed-off-by: Reinhold Degenfellner <rdegenfellner@gradle.com>
  • Loading branch information
reinsch82 committed Feb 1, 2023
1 parent 1a2ebf6 commit ab2afe9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public DefaultSettings(

private static String getProjectName(File settingsDir) {
if(contains(File.listRoots(), settingsDir)) {
return "generated " + settingsDir.toPath().getRoot().toString().replace("[\\:/]", "") + " " +
String rootIndicator = settingsDir.toPath().getRoot().toString().replaceAll("[\\\\:\\/]*", "");
return "generated " + rootIndicator + (rootIndicator.isEmpty() ? "" : " ") +
sha512().hashString(now().toString()).toString().substring(0, 6);
}
return settingsDir.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed 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.
*/

package org.gradle.initialization

import org.gradle.StartParameter
import org.gradle.api.initialization.dsl.ScriptHandler
import org.gradle.api.internal.GradleInternal
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.internal.initialization.ClassLoaderScope
import org.gradle.api.internal.initialization.ScriptHandlerFactory
import org.gradle.api.internal.plugins.DefaultPluginManager
import org.gradle.configuration.ScriptPluginFactory
import org.gradle.groovy.scripts.ScriptSource
import org.gradle.internal.buildoption.FeatureFlags
import org.gradle.internal.instantiation.InstantiatorFactory
import org.gradle.internal.management.DependencyResolutionManagementInternal
import org.gradle.internal.service.ServiceRegistry
import org.gradle.internal.service.scopes.ServiceRegistryFactory
import org.gradle.util.TestUtil
import spock.lang.Specification

class DefaultSettingsCommonTest extends Specification {

File settingsDir
StartParameter startParameter
ClassLoaderScope rootClassLoaderScope = Mock(ClassLoaderScope)
ClassLoaderScope classLoaderScope = Mock(ClassLoaderScope)
ScriptSource scriptSourceMock = Mock(ScriptSource)
GradleInternal gradleMock = Mock(GradleInternal)
ProjectDescriptorRegistry projectDescriptorRegistry = new DefaultProjectDescriptorRegistry()
ServiceRegistryFactory serviceRegistryFactory
FileResolver fileResolver = Mock(FileResolver)
ScriptPluginFactory scriptPluginFactory = Mock(ScriptPluginFactory)
ScriptHandlerFactory scriptHandlerFactory = Mock(ScriptHandlerFactory)
ScriptHandler settingsScriptHandler = Mock(ScriptHandler)
DefaultPluginManager pluginManager = Mock(DefaultPluginManager)
FeatureFlags previews = Mock(FeatureFlags)
DefaultSettings settings

def createSettings(String path = '/somepath/root') {
settingsDir = new File(path).absoluteFile
startParameter = new StartParameter(currentDir: new File(settingsDir, 'current'), gradleUserHomeDir: new File('gradleUserHomeDir'))

fileResolver.resolve(_) >> { args -> args[0].canonicalFile }

def settingsServices = Mock(ServiceRegistry)
settingsServices.get(FileResolver) >> fileResolver
settingsServices.get(ScriptPluginFactory) >> scriptPluginFactory
settingsServices.get(ScriptHandlerFactory) >> scriptHandlerFactory
settingsServices.get(ProjectDescriptorRegistry) >> projectDescriptorRegistry
settingsServices.get(FeatureFlags) >> previews
settingsServices.get(DefaultPluginManager) >>> [pluginManager, null]
settingsServices.get(InstantiatorFactory) >> Stub(InstantiatorFactory)
settingsServices.get(DependencyResolutionManagementInternal) >> Stub(DependencyResolutionManagementInternal)

serviceRegistryFactory = Mock(ServiceRegistryFactory) {
1 * createFor(_) >> settingsServices
}

def instantiator = TestUtil.instantiatorFactory().decorateLenient()
settings = instantiator.newInstance(DefaultSettings, serviceRegistryFactory,
gradleMock, classLoaderScope, rootClassLoaderScope, settingsScriptHandler,
settingsDir, scriptSourceMock, startParameter)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2009 the original author or authors.
*
* Licensed 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.
*/

package org.gradle.initialization

import org.gradle.internal.os.OperatingSystem


class DefaultSettingsRootFileSystemTest extends DefaultSettingsCommonTest {

def setup() {
createSettings('/')
}

def 'root project name generation in file system root'() {
expect:
settings.rootProject.name ==~ getRootProjectNamePattern()
}

private getRootProjectNamePattern() {
if(OperatingSystem.current().isWindows()) {
return 'generated [a-zA-Z] [a-z0-9]{6}'
}
return 'generated [a-z0-9]{6}'

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,16 @@

package org.gradle.initialization

import org.gradle.StartParameter

import org.gradle.api.Project
import org.gradle.api.UnknownProjectException
import org.gradle.api.initialization.Settings
import org.gradle.api.initialization.dsl.ScriptHandler
import org.gradle.api.internal.FeaturePreviewsActivationFixture
import org.gradle.api.internal.GradleInternal
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.internal.initialization.ClassLoaderScope
import org.gradle.api.internal.initialization.ScriptHandlerFactory
import org.gradle.api.internal.plugins.DefaultPluginManager
import org.gradle.configuration.ScriptPluginFactory
import org.gradle.groovy.scripts.ScriptSource
import org.gradle.internal.buildoption.FeatureFlags
import org.gradle.internal.instantiation.InstantiatorFactory
import org.gradle.internal.management.DependencyResolutionManagementInternal
import org.gradle.internal.service.ServiceRegistry
import org.gradle.internal.service.scopes.ServiceRegistryFactory
import org.gradle.util.TestUtil
import spock.lang.Specification

class DefaultSettingsTest extends Specification {
File settingsDir = new File('/somepath/root').absoluteFile
StartParameter startParameter = new StartParameter(currentDir: new File(settingsDir, 'current'), gradleUserHomeDir: new File('gradleUserHomeDir'))
ClassLoaderScope rootClassLoaderScope = Mock(ClassLoaderScope)
ClassLoaderScope classLoaderScope = Mock(ClassLoaderScope)
ScriptSource scriptSourceMock = Mock(ScriptSource)
GradleInternal gradleMock = Mock(GradleInternal)
ProjectDescriptorRegistry projectDescriptorRegistry = new DefaultProjectDescriptorRegistry()
ServiceRegistryFactory serviceRegistryFactory
FileResolver fileResolver = Mock(FileResolver)
ScriptPluginFactory scriptPluginFactory = Mock(ScriptPluginFactory)
ScriptHandlerFactory scriptHandlerFactory = Mock(ScriptHandlerFactory)
ScriptHandler settingsScriptHandler = Mock(ScriptHandler)
DefaultPluginManager pluginManager = Mock(DefaultPluginManager)
FeatureFlags previews = Mock(FeatureFlags)
DefaultSettings settings

class DefaultSettingsTest extends DefaultSettingsCommonTest {

def setup() {
fileResolver.resolve(_) >> { args -> args[0].canonicalFile }

def settingsServices = Mock(ServiceRegistry)
settingsServices.get(FileResolver) >> fileResolver
settingsServices.get(ScriptPluginFactory) >> scriptPluginFactory
settingsServices.get(ScriptHandlerFactory) >> scriptHandlerFactory
settingsServices.get(ProjectDescriptorRegistry) >> projectDescriptorRegistry
settingsServices.get(FeatureFlags) >> previews
settingsServices.get(DefaultPluginManager) >>> [pluginManager, null]
settingsServices.get(InstantiatorFactory) >> Stub(InstantiatorFactory)
settingsServices.get(DependencyResolutionManagementInternal) >> Stub(DependencyResolutionManagementInternal)

serviceRegistryFactory = Mock(ServiceRegistryFactory) {
1 * createFor(_) >> settingsServices
}

def instantiator = TestUtil.instantiatorFactory().decorateLenient()
settings = instantiator.newInstance(DefaultSettings, serviceRegistryFactory,
gradleMock, classLoaderScope, rootClassLoaderScope, settingsScriptHandler,
settingsDir, scriptSourceMock, startParameter)
createSettings()
}

def 'is wired properly'() {
Expand Down

0 comments on commit ab2afe9

Please sign in to comment.