c.s.tools.ide.analysis.modernize/test/unit/src/ide/analysis/modernize/TidyTestCase.java
author Ilia Gromov <ilia@netbeans.org>
Wed, 07 Jun 2017 20:23:29 +0300
branchrelease82
changeset 18423 b9d9af239a0c
parent 18403 ide.analysis.modernize/test/unit/src/ide/analysis/modernize/TidyTestCase.java@67d78b11d2e0
permissions -rw-r--r--
Fixing #270763 - Move clang-tidy integration to nb contrib
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - move wrapper
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - sign nbm
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - move tests
* * *
Fixing #270763 - Move clang-tidy integration to nb contrib - data for a new test
* * *
Fixed #270839 - [clang-tidy] Group checks in Editor hints
(transplanted from 35b6125ef00c470655dac6673075f5c12ec74593)
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * If you wish your version of this file to be governed by only the CDDL
    28  * or only the GPL Version 2, indicate your decision by adding
    29  * "[Contributor] elects to include this software in this distribution
    30  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    31  * single choice of license, a recipient has the option to distribute
    32  * your version of this file under either the CDDL, the GPL Version 2 or
    33  * to extend the choice of license to its licensees as provided above.
    34  * However, if you add GPL Version 2 code and therefore, elected the GPL
    35  * Version 2 license, then the option applies only if the new code is
    36  * made subject to such option by the copyright holder.
    37  *
    38  * Contributor(s):
    39  *
    40  * Portions Copyrighted 2011 Sun Microsystems, Inc.
    41  */
    42 package ide.analysis.modernize;
    43 
    44 import com.sun.tools.ide.analysis.modernize.impl.DiagnosticsTool;
    45 import com.sun.tools.ide.analysis.modernize.impl.ModernizeAnalyzerImpl;
    46 import com.sun.tools.ide.analysis.modernize.impl.ModernizeFix;
    47 import com.sun.tools.ide.analysis.modernize.impl.YamlParser;
    48 import com.sun.tools.ide.analysis.modernize.utils.AnalyticsTools;
    49 import java.io.File;
    50 import java.nio.file.Files;
    51 import java.nio.file.attribute.PosixFilePermissions;
    52 import java.util.ArrayList;
    53 import java.util.Arrays;
    54 import java.util.Collection;
    55 import java.util.Collections;
    56 import java.util.List;
    57 import java.util.logging.ConsoleHandler;
    58 import java.util.logging.Level;
    59 import java.util.logging.LogManager;
    60 import java.util.prefs.Preferences;
    61 import org.netbeans.api.progress.ProgressHandle;
    62 import org.netbeans.api.project.Project;
    63 import org.netbeans.api.project.ProjectManager;
    64 import org.netbeans.api.project.ui.OpenProjects;
    65 import org.netbeans.modules.analysis.SPIAccessor;
    66 import org.netbeans.modules.analysis.spi.Analyzer;
    67 import org.netbeans.modules.cnd.api.model.CsmFile;
    68 import org.netbeans.modules.cnd.api.model.CsmModel;
    69 import org.netbeans.modules.cnd.api.model.CsmModelAccessor;
    70 import org.netbeans.modules.cnd.api.model.CsmProject;
    71 import org.netbeans.modules.cnd.api.model.syntaxerr.AuditPreferences;
    72 import org.netbeans.modules.cnd.api.model.syntaxerr.CodeAudit;
    73 import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider;
    74 import org.netbeans.modules.cnd.api.project.NativeProject;
    75 import org.netbeans.modules.cnd.api.project.NativeProjectRegistry;
    76 import org.netbeans.modules.cnd.api.toolchain.CompilerSet;
    77 import org.netbeans.modules.cnd.api.toolchain.CompilerSetManager;
    78 import org.netbeans.modules.cnd.makeproject.api.MakeProject;
    79 import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
    80 import org.netbeans.modules.cnd.modelutil.CsmUtilities;
    81 import org.netbeans.modules.cnd.test.CndBaseTestCase;
    82 import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment;
    83 import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory;
    84 import org.netbeans.modules.refactoring.api.Scope;
    85 import org.openide.filesystems.FileObject;
    86 import org.openide.filesystems.FileUtil;
    87 import org.openide.util.Exceptions;
    88 
    89 /**
    90  *
    91  * @author Ilia Gromov
    92  */
    93 public abstract class TidyTestCase extends CndBaseTestCase {
    94 
    95     private final String projectName;
    96 
    97     private List<FileObject> fileObjects;
    98     private MakeProject project;
    99     private FileObject projectDirFileObject;
   100 
   101     private final String CLANG_TIDY_PATH = "clang-tidy-3.9p";
   102 
   103     public TidyTestCase(String projectName) {
   104         super(projectName);
   105 
   106         this.projectName = projectName;
   107     }
   108 
   109     protected static CodeAudit findAudit(Collection<CodeAudit> audits, String id) {
   110         for (CodeAudit audit : audits) {
   111             if (audit.getID().equals(id)) {
   112                 return audit;
   113             }
   114         }
   115         return null;
   116     }
   117 
   118     @Override
   119     protected void setUp() throws Exception {
   120         super.setUp();
   121         LogManager.getLogManager().reset();
   122 
   123         java.util.logging.Logger LOG = java.util.logging.Logger.getLogger("ide.analysis.tidy");
   124         LOG.setLevel(Level.ALL);
   125 
   126         ConsoleHandler consoleHandler = new ConsoleHandler();
   127         consoleHandler.setLevel(Level.ALL);
   128         LOG.addHandler(consoleHandler);
   129 
   130         File clangTidyFile = getBinaryFile();
   131         Files.setPosixFilePermissions(clangTidyFile.toPath(), PosixFilePermissions.fromString("rwxrwxrwx"));
   132 
   133         assertTrue(getBinaryFile().exists());
   134 
   135         File projectDirFile = getDataFile(projectName);
   136         projectDirFileObject = FileUtil.toFileObject(projectDirFile);
   137 
   138         fileObjects = new ArrayList<FileObject>();
   139 
   140         project = (MakeProject) ProjectManager.getDefault().findProject(projectDirFileObject);
   141 
   142         OpenProjects.getDefault().open(new Project[]{project}, false);
   143         while (!OpenProjects.getDefault().isProjectOpen(project)) {
   144             try {
   145                 Thread.sleep(100);
   146             } catch (InterruptedException ex) {
   147                 Exceptions.printStackTrace(ex);
   148             }
   149         }
   150 
   151         CsmModel model = CsmModelAccessor.getModel();
   152         assertNotNull(project);
   153 
   154         NativeProject np = project.getLookup().lookup(NativeProject.class);
   155         NativeProjectRegistry.getDefault().register(np);
   156         model.enableProject(np);
   157         assertNotNull(np);
   158 
   159         CsmProject csmProject = model.getProject(np);
   160         assertNotNull(csmProject);
   161 
   162         csmProject.waitParse();
   163 
   164     }
   165 
   166     public ExecutionEnvironment getExecEnv() {
   167         ExecutionEnvironment execEnv = ExecutionEnvironmentFactory.getLocal();
   168         return execEnv;
   169     }
   170 
   171     public CompilerSet getCompilerSet() {
   172         CompilerSetManager csm = CompilerSetManager.get(getExecEnv());
   173         return csm.getDefaultCompilerSet();
   174     }
   175 
   176     public MakeProject getProject() {
   177         return project;
   178     }
   179 
   180     public FileObject getProjectDir() {
   181         return projectDirFileObject;
   182     }
   183 
   184     public FileObject getSourceFile(String name) {
   185         return FileUtil.toFileObject(new File(FileUtil.toFile(getProjectDir()), name));
   186     }
   187 
   188     public File getBinaryFile() {
   189         return new File(getDataDir(), CLANG_TIDY_PATH);
   190     }
   191 
   192     protected final void performTest(String startFileName, String checkName, boolean isStartFile) {
   193         try {
   194             FileObject startFile = getSourceFile(startFileName);
   195             assertTrue(startFile.isValid());
   196 
   197             Scope scope = Scope.create(null, null, Arrays.asList(new FileObject[]{startFile}));
   198             Analyzer.Context context = SPIAccessor.ACCESSOR.createContext(scope, null, null, ProgressHandle.createHandle("dummy"), 0, 0);
   199             Analyzer.AnalyzerFactory factory = new ModernizeAnalyzerImpl.AnalyzerFactoryImpl();
   200 
   201             ModernizeAnalyzerImpl analyzer = (ModernizeAnalyzerImpl) factory.createAnalyzer(context);
   202             ExecutionEnvironment execEnv = ExecutionEnvironmentFactory.getLocal();
   203             final CsmFile csmFile = CsmUtilities.getCsmFile(startFile, true, false);
   204             Item item = AnalyticsTools.findItem(csmFile, getProject());
   205 
   206             Collection<CodeAudit> audits = DiagnosticsTool.getAudits(getBinaryFile().getAbsolutePath(), execEnv, new AuditPreferences(Preferences.userNodeForPackage(this.getClass())));
   207             CodeAudit audit = findAudit(audits, checkName);
   208             assertNotNull(audit);
   209 
   210             // ----- BEGIN diagnostics -----
   211             DiagnosticsTool diagnosticsTool = new DiagnosticsTool(execEnv, item, getProject(), getBinaryFile().getAbsolutePath());
   212 
   213             int exitCode = diagnosticsTool.process(Collections.singleton(audit.getID()), csmFile, true);
   214             if (exitCode != DiagnosticsTool.STATUS_OK) {
   215                 fail();
   216             }
   217 
   218             List<YamlParser.Diagnostics> diags = YamlParser.getDefault().parseYaml(diagnosticsTool.getYamlAsString());
   219             List<ModernizeFix> fixes = new ArrayList<ModernizeFix>();
   220 
   221             CsmErrorProvider.Request request = new RequestImpl(csmFile);
   222             CsmErrorProvider.Response response = new ResponseImpl(fixes);
   223 
   224             analyzer.getErrorProvider(null).postProcess(false, csmFile, getProject(), diags, request, response);
   225             // ----- END diagnostics -----
   226 
   227             processTestResults(fixes, startFileName);
   228         } catch (Exception ex) {
   229             ex.printStackTrace(System.err);
   230             fail(ex.toString());
   231         }
   232     }
   233 
   234     protected void processTestResults(List<ModernizeFix> fixes, String startFileName) throws Exception {
   235 
   236     }
   237 }