Tests for New File Action

How do I test a New File action in the NewGroup, including bringing up the Dialog, selecting the the language, checking the validation and then checking that the file is created in the correct directory?  Specifically, New Elixir Module should place in Foo in foo.ex, FooBar in foo_bar.ex, Foo.Bar in foo/bar.ex.  From other forum posts, I think I should be using LightCodeInsightFixtureTestCase or CodeInsightFixtureTestCase, but I don't have any fixture to start out with since I'm testing file creation.  https://devnet.jetbrains.com/message/5536514#5536514 is close, but I don't have a starting file from which the new file is created.  I'm sure something used so much as testing New File is somewhere in the forums or documented since every custom language plugin would need it, but I'm not having any luck with the search.

5 comments
Comment actions Permalink

Hi Luke,

In IntelliJ IDEA, we usually do not test the user interface code. Because of that, you won't find any test framework support for testing the dialog itself. However, you certainly can, and should, test the actual file creation logic that happens in your action. You can use LightPlatformCodeInsightFixtureTestCase as the base class for your test: the fixture will set up a project and module for you, and you can create the file through your own code and verify the result through CodeInsightTextFixture.checkResultByFile().

0
Comment actions Permalink

How do I get an PSIDirectory from the myFixture or myModule so I can call createFile(String name, String templateName, PsiDirectory dir)   In the actual IDE, using the debugger, I can see that CreateFormTemplateAction.actionPerformed(AnActionEvent) uses AnActionEvent.getDataContext -> LangDataKeys.IDE_VIEW.getData(dataContext) -> IdeView.getOrChooseDirectory() -> PsiDirectory, so can I get the IdeView in the test or some other method of getting a PsiDirectory?  None of the methods that `.` shows me on myFixture or myModule in IntelliJ return a PsiDirectory.

0
Comment actions Permalink

CodeInsightTestFixture.getTempDirFixture().findOrCreateDir("") returns the root directory of the temporary in-memory project used by the tests. If you need a directory other than the root, you can pass a non-empty path to that method. Once you have a VirtualFile for the directory, you can use CodeInsightTestFixture.getPsiManager().findDirectory() to obtain the corresponding PsiDirectory.

0
Comment actions Permalink

Thanks you for that last post.  I have the my action CreateElixiroduleAction.createFile returning an ElixirFile as expected, but checkResultsByFile is raising an exception.

 
public void testCamelCaseAliasIsLowerCaseUnderscored() throws IOException {
    ActionManager actionManager = ActionManager.getInstance();
    
CreateElixirModuleAction elixirNewFileAction = (CreateElixirModuleAction) actionManager.getAction("Elixir.NewFile");

    // @see https://devnet.jetbrains.com/message/5539349#5539349
    
VirtualFile directoryVirtualFile = myFixture.getTempDirFixture().findOrCreateDir("");
    
PsiDirectory directory = myFixture.getPsiManager().findDirectory(directoryVirtualFile);

    
ElixirFile file = elixirNewFileAction.createFile("FooBar", TEMPLATE_NAME, directory);
    assertNotNull
("Expected CreateElixirModuleActionTest.createFile to create an ElixirFile", file);

    boolean
ignoreTrailingWhitespaces = false;

    myFixture
.checkResultByFile(
            "foo_bar.ex",
            "foo_bar.ex",
            
ignoreTrailingWhitespaces
    );
}


raises

java.lang.IllegalArgumentException: Argument for @NotNull parameter 'editor' of com/intellij/openapi/editor/ex/util/EditorUtil.fillVirtualSpaceUntilCaret must not be null
     at com.intellij.openapi.editor.ex.util.EditorUtil.fillVirtualSpaceUntilCaret(EditorUtil.java)
     at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.a(CodeInsightTestFixtureImpl.java:1823)
     at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.access$1000(CodeInsightTestFixtureImpl.java:128)
     at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl$20.run(CodeInsightTestFixtureImpl.java:1198)
     at com.intellij.util.ui.UIUtil.invokeAndWaitIfNeeded(UIUtil.java:2398)
     at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.checkResultByFile(CodeInsightTestFixtureImpl.java:1187)
     at org.elixir_lang.action.CreateElixirModuleActionTest.testCamelCaseAliasIsLowerCaseUnderscored(CreateElixirModuleActionTest.java:34)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at com.intellij.testFramework.UsefulTestCase.access$001(UsefulTestCase.java:82)
     at com.intellij.testFramework.UsefulTestCase$2.run(UsefulTestCase.java:302)
     at com.intellij.util.ui.UIUtil.invokeAndWaitIfNeeded(UIUtil.java:2398)
     at com.intellij.testFramework.UsefulTestCase.invokeTestRunnable(UsefulTestCase.java:334)
     at com.intellij.testFramework.UsefulTestCase.runTest(UsefulTestCase.java:318)
     at com.intellij.testFramework.UsefulTestCase.defaultRunBare(UsefulTestCase.java:346)
     at com.intellij.testFramework.UsefulTestCase$4.run(UsefulTestCase.java:428)
     at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
     at java.awt.EventQueue.access$400(EventQueue.java:82)
     at java.awt.EventQueue$2.run(EventQueue.java:676)
     at java.awt.EventQueue$2.run(EventQueue.java:674)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
     at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:362)
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


How should I be populating the `myEditor` field, which is null when I'm calling checkResultsByFile now.

0
Comment actions Permalink

Looking at the code, the myEditor is only needed when `ignoreTrailingWhitespaces` is `false`, so I
changed it to `true` and made sure to override `getTestDataPath()` and the test is working now:

 
package org.elixir_lang.action;

import
com.intellij.openapi.actionSystem.ActionManager;
import
com.intellij.openapi.vfs.VirtualFile;
import
com.intellij.psi.PsiDirectory;
import
com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import
org.elixir_lang.psi.ElixirFile;

import
java.io.IOException;

/**
* @see <a href="https://confluence.jetbrains.com/display/IntelliJIDEA/Completion+Test">https://confluence.jetbrains.com/display/IntelliJIDEA/Completion+Test"</a>
*/
public class
CreateElixirModuleActionTest extends LightCodeInsightFixtureTestCase {
    public static final String TEMPLATE_NAME = "Elixir Module";

    public void testCamelCaseAliasIsLowerCaseUnderscored
() throws IOException {
        ActionManager actionManager = ActionManager.getInstance();
        
CreateElixirModuleAction elixirNewFileAction = (CreateElixirModuleAction) actionManager.getAction("Elixir.NewFile");

        // @see https://devnet.jetbrains.com/message/5539349#5539349
        
VirtualFile directoryVirtualFile = myFixture.getTempDirFixture().findOrCreateDir("");
        
PsiDirectory directory = myFixture.getPsiManager().findDirectory(directoryVirtualFile);

        
ElixirFile file = elixirNewFileAction.createFile("FooBar", TEMPLATE_NAME, directory);
        assertNotNull
("Expected CreateElixirModuleAction.createFile to create an ElixirFile", file);

        boolean
ignoreTrailingWhitespaces = true;

        myFixture
.checkResultByFile(
                "foo_bar.ex",
                "foo_bar.ex",
                
ignoreTrailingWhitespaces
        );
    
}

    @Override
    protected
String getTestDataPath() {
        return "testData/org/elixir_lang/action/create_elixir_module_action_test";
    
}
}


However, it only works when tested in IntelliJ.  When I run it it in ant for travis-ci, it fails, but I'll open a new question for that since I had other issues with ant running inspection tests.

0

Please sign in to leave a comment.