Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #41 from koxudaxi/add_unittest
Browse files Browse the repository at this point in the history
add unittest
  • Loading branch information
koxudaxi committed May 27, 2020
2 parents e148a32 + 00a5c4e commit 144e5ea
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 14 deletions.
4 changes: 0 additions & 4 deletions README.md
Expand Up @@ -37,10 +37,6 @@ You can install the stable version on PyCharm's `Marketplace` (Preference -> Plu

## TODO
- add actions on context-menu
- add buttons to install on left-side of a editor
- publish this plugin on JetBrains' plugin market
- CI/CD
- unittest
- document

## Feature Restrictions
Expand Down
4 changes: 0 additions & 4 deletions docs/index.md
Expand Up @@ -35,10 +35,6 @@ You can install the stable version on PyCharm's `Marketplace` (Preference -> Plu

## TODO
- add actions on context-menu
- add buttons to install on left-side of a editor
- publish this plugin on JetBrains' plugin market
- CI/CD
- unittest
- document

## Feature Restrictions
Expand Down
8 changes: 4 additions & 4 deletions resources/META-INF/plugin.xml
Expand Up @@ -5,6 +5,10 @@
<vendor email="koaxudai@gmail.com">Koudai Aono @koxudaxi</vendor>
<change-notes><![CDATA[
<h2>version 0.0.9</h2>
<p>Features</p>
<ul>
<li>Add unittest [#41] </li>
</ul>
<p>Bug fixes</p>
<ul>
<li>Fix an error not found toml plugin [#42] </li>
Expand Down Expand Up @@ -81,10 +85,6 @@
<li>install extras by clicking a line marker (<a href="https://plugins.jetbrains.com/plugin/8195-toml">Toml plugin</a> is required)</li>
<h3>TODO</h3>
<li>add actions on context-menu</li>
<li>add buttons to install on left-side of a editor</li>
<li>publish this plugin on JetBrains' plugin market</li>
<li>CI/CD</li>
<li>unittest</li>
<li>document</li>
]]></description>

Expand Down
4 changes: 2 additions & 2 deletions src/com/koxudaxi/poetry/PyPoetryPackageManager.kt
Expand Up @@ -148,7 +148,7 @@ class PyPoetryPackageManager(val sdk: Sdk) : PyPackageManager() {
/**
* Parses the output of `poetry install --dry-run ` into a list of packages.
*/
private fun parsePoetryInstallDryRun(input: String): Pair<List<PyPackage>, List<PyRequirement>> {
fun parsePoetryInstallDryRun(input: String): Pair<List<PyPackage>, List<PyRequirement>> {
fun getNameAndVersion(line: String): Pair<String, String> {
return line.split(" ").let {
Pair(it[4], it[5].replace(Regex("[()]"), ""))
Expand All @@ -159,7 +159,7 @@ class PyPoetryPackageManager(val sdk: Sdk) : PyPackageManager() {
val pyRequirements = mutableListOf<PyRequirement>()
input
.lineSequence()
.filter { it.endsWith(")") }
.filter { it.endsWith(")") || it.endsWith("Already installed") }
.forEach { line ->
getNameAndVersion(line).also {
when {
Expand Down
14 changes: 14 additions & 0 deletions testData/Poetry/getPyProjectTomlForPoetry/pyproject.toml
@@ -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"
@@ -0,0 +1 @@
{"name": "unittest"}
@@ -0,0 +1,4 @@
[tool.invalid]
name = "unittest"
version = "0.1.0"
description = ""
@@ -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 testSrc/com/jetbrains/python/fixtures/PythonMockSdk.java
@@ -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;
}
}
}
30 changes: 30 additions & 0 deletions testSrc/com/jetbrains/python/fixtures/PythonTestUtil.java
@@ -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";
}
}
31 changes: 31 additions & 0 deletions testSrc/com/koxudaxi/poetry/PoetryTest.kt
@@ -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)
}
}
21 changes: 21 additions & 0 deletions testSrc/com/koxudaxi/poetry/PoetryTestCase.kt
@@ -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()
}
}

39 changes: 39 additions & 0 deletions testSrc/com/koxudaxi/poetry/PyPoetryPackageManagerTest.kt
@@ -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")
)
)

}
}

0 comments on commit 144e5ea

Please sign in to comment.