This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from koxudaxi/add_unittest
add unittest
- Loading branch information
Showing
14 changed files
with
258 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[tool.poetry] | ||
name = "unittest" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["Koudai Aono <koxudaxi@gmail.com>"] | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.7" | ||
|
||
[tool.poetry.extras] | ||
|
||
[build-system] | ||
requires = ["poetry>=0.12"] | ||
build-backend = "poetry.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name": "unittest"} |
4 changes: 4 additions & 0 deletions
4
testData/Poetry/getPyProjectTomlForPoetryInvalid/pyproject.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[tool.invalid] | ||
name = "unittest" | ||
version = "0.1.0" | ||
description = "" |
9 changes: 9 additions & 0 deletions
9
testData/PyPoetryPackageManager/parsePoetryInstallDryRun/dry-run-result.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Installing dependencies from lock file | ||
|
||
|
||
Package operations: 3 installs, 0 updates, 0 removals, 1 skipped | ||
|
||
- Installing mypy-extensions (0.4.3) | ||
- Skipping typed-ast (1.4.1) Already installed | ||
- Installing typing-extensions (3.7.4.2) | ||
- Installing mypy (0.770) |
103 changes: 103 additions & 0 deletions
103
testSrc/com/jetbrains/python/fixtures/PythonMockSdk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
package com.jetbrains.python.fixtures; | ||
|
||
import com.intellij.openapi.projectRoots.Sdk; | ||
import com.intellij.openapi.projectRoots.SdkAdditionalData; | ||
import com.intellij.openapi.projectRoots.SdkTypeId; | ||
import com.intellij.openapi.projectRoots.impl.MockSdk; | ||
import com.intellij.openapi.roots.OrderRootType; | ||
import com.intellij.openapi.vfs.LocalFileSystem; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.util.containers.ContainerUtil; | ||
import com.intellij.util.containers.MultiMap; | ||
import com.jetbrains.python.PyNames; | ||
import com.jetbrains.python.codeInsight.typing.PyTypeShed; | ||
import com.jetbrains.python.codeInsight.userSkeletons.PyUserSkeletonsUtil; | ||
import com.jetbrains.python.psi.LanguageLevel; | ||
import com.jetbrains.python.sdk.PythonSdkUtil; | ||
import org.jdom.Element; | ||
import org.jetbrains.annotations.NonNls; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* @author yole | ||
*/ | ||
public class PythonMockSdk { | ||
@NonNls private static final String MOCK_SDK_NAME = "Mock Python SDK"; | ||
|
||
private PythonMockSdk() { | ||
} | ||
|
||
public static Sdk create(final String version, final VirtualFile @NotNull ... additionalRoots) { | ||
final String mock_path = PythonTestUtil.getTestDataPath() + "/MockSdk" + version + "/"; | ||
|
||
String sdkHome = new File(mock_path, "bin/python" + version).getPath(); | ||
|
||
MultiMap<OrderRootType, VirtualFile> roots = MultiMap.create(); | ||
OrderRootType classes = OrderRootType.CLASSES; | ||
|
||
ContainerUtil.putIfNotNull(classes, LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(mock_path, "Lib")), roots); | ||
|
||
ContainerUtil.putIfNotNull(classes, PyUserSkeletonsUtil.getUserSkeletonsDirectory(), roots); | ||
|
||
final LanguageLevel level = LanguageLevel.fromPythonVersion(version); | ||
final VirtualFile typeShedDir = PyTypeShed.INSTANCE.getDirectory(); | ||
PyTypeShed.INSTANCE | ||
.findRootsForLanguageLevel(level) | ||
.forEach(path -> ContainerUtil.putIfNotNull(classes, typeShedDir.findFileByRelativePath(path), roots)); | ||
|
||
String mock_stubs_path = mock_path + PythonSdkUtil.SKELETON_DIR_NAME; | ||
ContainerUtil.putIfNotNull(classes, LocalFileSystem.getInstance().refreshAndFindFileByPath(mock_stubs_path), roots); | ||
|
||
roots.putValues(classes, Arrays.asList(additionalRoots)); | ||
|
||
MockSdk sdk = new MockSdk(MOCK_SDK_NAME + " " + version, sdkHome, "Python " + version + " Mock SDK", roots, new PyMockSdkType(version)); | ||
|
||
// com.jetbrains.python.psi.resolve.PythonSdkPathCache.getInstance() corrupts SDK, so have to clone | ||
return sdk.clone(); | ||
} | ||
|
||
private static class PyMockSdkType implements SdkTypeId { | ||
|
||
@NotNull | ||
private final String myVersionString; | ||
private final String mySdkIdName; | ||
|
||
private PyMockSdkType(@NotNull String string) { | ||
mySdkIdName = PyNames.PYTHON_SDK_ID_NAME; | ||
myVersionString = string; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getName() { | ||
return mySdkIdName; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getVersionString(@NotNull Sdk sdk) { | ||
return myVersionString; | ||
} | ||
|
||
@Override | ||
public void saveAdditionalData(@NotNull SdkAdditionalData additionalData, @NotNull Element additional) { | ||
|
||
} | ||
|
||
@Nullable | ||
@Override | ||
public SdkAdditionalData loadAdditionalData(@NotNull Sdk currentSdk, @NotNull Element additional) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean isLocalSdk(@NotNull Sdk sdk) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2000-2013 JetBrains s.r.o. | ||
* | ||
* 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 com.jetbrains.python.fixtures; | ||
|
||
import com.jetbrains.python.PythonHelpersLocator; | ||
|
||
/** | ||
* @author yole | ||
*/ | ||
public class PythonTestUtil { | ||
private PythonTestUtil() { | ||
} | ||
|
||
public static String getTestDataPath() { | ||
return PythonHelpersLocator.getPythonCommunityPath() + "/testData"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.koxudaxi.poetry | ||
|
||
|
||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.openapi.vfs.impl.local.LocalFileSystemImpl | ||
import org.jetbrains.kotlin.konan.file.File | ||
|
||
|
||
class PoetryTest : PoetryTestCase() { | ||
private val testFile: VirtualFile | ||
get() { | ||
return getTestData("pyproject.toml") | ||
} | ||
fun testGetPyProjectTomlForPoetry() { | ||
val result = getPyProjectTomlForPoetry(testFile) | ||
assertEquals(result.first, 0) | ||
assertEquals(result.second, testFile) | ||
} | ||
|
||
fun testGetPyProjectTomlForPoetryInvalid() { | ||
val result = getPyProjectTomlForPoetry(testFile) | ||
assertEquals(result.first, 0) | ||
assertEquals(result.second, null) | ||
} | ||
|
||
fun testGetPyProjectTomlForPoetryBroken() { | ||
val result = getPyProjectTomlForPoetry(testFile) | ||
assertEquals(result.first, 0) | ||
assertEquals(result.second, null) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.koxudaxi.poetry | ||
|
||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.openapi.vfs.impl.local.LocalFileSystemImpl | ||
import com.intellij.testFramework.fixtures.BasePlatformTestCase | ||
import org.jetbrains.kotlin.konan.file.File | ||
|
||
abstract class PoetryTestCase : BasePlatformTestCase() { | ||
protected open val testClassName: String = this.javaClass.simpleName.replace("Test", "") | ||
protected val dataDir: String | ||
get() { | ||
return "testData" + File.separator + testClassName + File.separator + getTestName(true) | ||
} | ||
fun getTestData(fileName: String): VirtualFile { | ||
return LocalFileSystemImpl.getInstance().findFileByPath(dataDir + File.separator + fileName)!! | ||
} | ||
fun getTestDataAsText(fileName: String): String { | ||
return getTestData(fileName).inputStream.bufferedReader().readText() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.koxudaxi.poetry | ||
|
||
|
||
import com.jetbrains.python.fixtures.PythonMockSdk | ||
import com.jetbrains.python.packaging.PyPackage | ||
import com.jetbrains.python.packaging.PyRequirement | ||
import com.jetbrains.python.packaging.PyRequirementParser | ||
|
||
|
||
class PyPoetryPackageManagerTest : PoetryTestCase() { | ||
private val testDataAsText: String | ||
get() { | ||
return getTestDataAsText("dry-run-result.txt") | ||
} | ||
private fun getPyPackage(name: String, version: String) :PyPackage { | ||
return PyPackage(name, version, null, emptyList()) | ||
} | ||
fun getPyRequirement(name: String, version: String): PyRequirement? { | ||
return PyRequirementParser.fromLine("${name}==${version}") | ||
} | ||
fun testParsePoetryInstallDryRun() { | ||
val sdk = PythonMockSdk.create("3.7") | ||
val pyPoetryPackageManager = PyPoetryPackageManager(sdk) | ||
val result = pyPoetryPackageManager.parsePoetryInstallDryRun(testDataAsText) | ||
assertEquals(result.first.size, 1) | ||
assertEquals(result.first, | ||
listOf(getPyPackage("typed-ast", "1.4.1")) | ||
) | ||
assertEquals(result.second.size, 3) | ||
assertEquals(result.second, | ||
listOf( | ||
getPyRequirement("mypy-extensions","0.4.3"), | ||
getPyRequirement("typing-extensions","3.7.4.2"), | ||
getPyRequirement("mypy","0.770") | ||
) | ||
) | ||
|
||
} | ||
} |