Package com.puppetlabs.geppetto.ruby

Examples of com.puppetlabs.geppetto.ruby.RubyHelper


        return p;
    return null;
  }

  private void performLoad(File distroDir, File pluginsDir, File tptpFile) throws Exception {
    RubyHelper helper = new RubyHelper();
    helper.setUp();
    try {
      TargetEntry target = helper.loadDistroTarget(distroDir);

      // Load the variables in the settings:: namespace
      helper.loadSettings(target);

      // Load the default meta variables (available as local in every scope).
      helper.loadMetaVariables(target);
      helper.loadPuppetVariables(target);

      for(Type t : target.getTypes())
        System.err.println("Found t: " + t.getName());
      for(Function f : target.getFunctions())
        System.err.println("Found f: " + f.getName());

      // Load (optional) any plugins
      List<TargetEntry> plugins = helper.loadPluginsTarget(pluginsDir);

      // Save the TargetEntry as a loadable resource
      ResourceSet resourceSet = new ResourceSetImpl();
      URI fileURI = URI.createFileURI(tptpFile.getAbsolutePath());
      Resource targetResource = resourceSet.createResource(fileURI);

      // Add all (optional) plugins
      targetResource.getContents().add(target);
      for(TargetEntry entry : plugins)
        targetResource.getContents().add(entry);
      targetResource.save(null);
      System.err.println("Target saved to: " + fileURI.toString());

    }
    finally {
      helper.tearDown();
    }

  }
View Full Code Here


  }

  @Test
  public void testLoadMockDistro() throws Exception {
    File distroDir = TestDataProvider.getTestFile(new Path("testData/mock-puppet-distro/puppet-2.6.2_0/lib/puppet"));
    RubyHelper helper = new RubyHelper();
    helper.setUp();
    try {
      TargetEntry target = helper.loadDistroTarget(distroDir);

      // check the target itself
      assertNotNull("Should have resultet in a TargetEntry", target);
      assertEquals("Should have defined description", "Puppet Distribution", target.getDescription());
      assertEquals("Should have defined name", "puppet", target.getLabel());
      assertEquals("Should have defined version", "2.6.2_0", target.getVersion());

      // should have found one type "mocktype"
      assertEquals("Should have found one type", 1, target.getTypes().size());
      Type type = target.getTypes().get(0);
      assertEquals("Should have found 'mocktype'", "mocktype", type.getName());
      assertEquals("Should have found documentation", "<p>This is a mock type</p>", type.getDocumentation());

      assertEquals("Should have one property", 1, type.getProperties().size());
      {
        Property prop = getProperty("prop1", type);
        assertNotNull("Should have a property 'prop1", prop);
        assertEquals("Should have defined documentation", "<p>This is property1</p>", prop.getDocumentation());
      }
      {
        assertEquals("Should have one parameter", 1, type.getParameters().size());
        Parameter param = getParameter("param1", type);
        assertNotNull("Should have a parameter 'param1", param);
        assertEquals("Should have defined documentation", "<p>This is parameter1</p>", param.getDocumentation());
      }

      // There should be two type fragments, with a contribution each
      List<TypeFragment> typeFragments = target.getTypeFragments();
      assertEquals("Should have found two fragments", 2, typeFragments.size());

      TypeFragment fragment1 = typeFragments.get(0);
      TypeFragment fragment2 = typeFragments.get(1);
      boolean fragment1HasExtra1 = getProperty("extra1", fragment1) != null;
      {
        Property prop = getProperty("extra1", fragment1HasExtra1
            ? fragment1
            : fragment2);
        assertNotNull("Should have a property 'extra1", prop);
        assertEquals(
          "Should have defined documentation", "<p>An extra property called extra1</p>",
          prop.getDocumentation());
      }
      {
        Property prop = getProperty("extra2", fragment1HasExtra1
            ? fragment2
            : fragment1);
        assertNotNull("Should have a property 'extra2", prop);
        assertEquals(
          "Should have defined documentation", "<p>An extra property called extra2</p>",
          prop.getDocumentation());
      }

      // should have found two functions "echotest" and "echotest2"
      // and the log functions (8)
      assertEquals("Should have found two functions", 10, target.getFunctions().size());
      {
        Function f = getFunction("echotest", target);
        assertNotNull("Should have found function 'echotest'", f);
        assertTrue("echotest should be an rValue", f.isRValue());
      }
      {
        Function f = getFunction("echotest2", target);
        assertNotNull("Should have found function 'echotest2'", f);
        assertFalse("echotest2 should not be an rValue", f.isRValue());
      }

    }
    finally {
      helper.tearDown();
    }
  }
View Full Code Here

public class SmokeTest {

  @Test
  public void testConfiguration() {
    RubyHelper rubyHelper = new RubyHelper();
    rubyHelper.setUp();
    assertTrue("Should have a real ruby service configured", rubyHelper.isRubyServicesAvailable());
  }
View Full Code Here

  }

  @Test
  public void testHelloBrokenWorld() throws Exception {
    File aRubyFile = TestDataProvider.getTestFile(new Path("testData/ruby/helloBrokenWorld.rb"));
    RubyHelper helper = new RubyHelper();
    helper.setUp();
    try {
      IRubyParseResult r = helper.parse(aRubyFile);
      assertEquals("Expect one error", 1, r.getIssues().size());
      IRubyIssue theIssue = r.getIssues().get(0);
      assertTrue("Expect one syntax error", theIssue.isSyntaxError());
      assertEquals("source line starts with 1", 1, theIssue.getLine());
      assertEquals("the file path is reported", aRubyFile.getPath(), theIssue.getFileName());
      assertTrue("the error message is the expected", theIssue.getMessage().contains("unexpected tLPAREN_ARG"));
      // assertTrue("the error message is the expected", theIssue.getMessage().startsWith("syntax error, unexpected tLPAREN_ARG"));
    }
    finally {
      helper.tearDown();
    }
  }
View Full Code Here

  }

  @Test
  public void testHelloBrokenWorld2() throws Exception {
    File aRubyFile = TestDataProvider.getTestFile(new Path("testData/ruby/helloBrokenWorld2.rb"));
    RubyHelper helper = new RubyHelper();
    helper.setUp();
    try {
      IRubyParseResult r = helper.parse(aRubyFile);
      assertEquals("Expect one error", 1, r.getIssues().size());
      IRubyIssue theIssue = r.getIssues().get(0);
      assertTrue("Expect one syntax error", theIssue.isSyntaxError());
      assertEquals("source line is 2", 2, theIssue.getLine());
      assertEquals("the file path is reported", aRubyFile.getPath(), theIssue.getFileName());
      assertTrue("the error message is the expected", theIssue.getMessage().contains("unexpected tLPAREN_ARG"));
    }
    finally {
      helper.tearDown();
    }
  }
View Full Code Here

  }

  @Test
  public void testHelloWorld() throws Exception {
    File aRubyFile = TestDataProvider.getTestFile(new Path("testData/ruby/helloWorld.rb"));
    RubyHelper helper = new RubyHelper();
    helper.setUp();
    helper.parse(aRubyFile);
    helper.tearDown();
  }
View Full Code Here

  protected void internalLoadRuby(InputStream inputStream) throws IOException {
    if(loadType == LoadType.IGNORED) {
      this.getContents().clear();
      return;
    }
    RubyHelper helper = new RubyHelper();
    helper.setUp();

    URI uri = getURI();
    try {
      switch(loadType) {
        case TYPE: {
          List<PPTypeInfo> typeInfo = helper.getTypeInfo(uri.path(), new InputStreamReader(inputStream));
          for(PPTypeInfo info : typeInfo) {
            Type type = PPTPFactory.eINSTANCE.createType();
            type.setName(info.getTypeName());
            type.setDocumentation(info.getDocumentation());
            for(Map.Entry<String, PPTypeInfo.Entry> entry : info.getParameters().entrySet()) {
              Parameter parameter = PPTPFactory.eINSTANCE.createParameter();
              parameter.setName(entry.getKey());
              parameter.setDocumentation(entry.getValue().documentation);
              parameter.setRequired(entry.getValue().isRequired());
              type.getParameters().add(parameter);
            }
            for(Map.Entry<String, PPTypeInfo.Entry> entry : info.getProperties().entrySet()) {
              Property property = PPTPFactory.eINSTANCE.createProperty();
              property.setName(entry.getKey());
              property.setDocumentation(entry.getValue().documentation);
              property.setRequired(entry.getValue().isRequired());
              type.getProperties().add(property);
            }
            getContents().add(type);
          }
        }
          break;

        case FUNCTION: {
          List<PPFunctionInfo> functions = helper.getFunctionInfo(uri.path(), new InputStreamReader(
            inputStream));

          for(PPFunctionInfo info : functions) {
            Function pptpFunc = PPTPFactory.eINSTANCE.createFunction();
            pptpFunc.setName(info.getFunctionName());
            pptpFunc.setRValue(info.isRValue());
            pptpFunc.setDocumentation(info.getDocumentation());
            getContents().add(pptpFunc);
          }
        }
          break;

        case META: {
          PPTypeInfo info = helper.getMetaTypeInfo(uri.path(), new InputStreamReader(inputStream));

          MetaType type = PPTPFactory.eINSTANCE.createMetaType();
          type.setName(info.getTypeName());
          type.setDocumentation(info.getDocumentation());
          for(Map.Entry<String, PPTypeInfo.Entry> entry : info.getParameters().entrySet()) {
            Parameter parameter = PPTPFactory.eINSTANCE.createParameter();
            parameter.setName(entry.getKey());
            parameter.setDocumentation(entry.getValue().documentation);
            parameter.setRequired(entry.getValue().isRequired());
            type.getParameters().add(parameter);
          }
          // TODO: Scan the puppet source for providers for the type
          // This is a CHEAT -
          // https://github.com/puppetlabs/geppetto/issues/37
          Parameter p = PPTPFactory.eINSTANCE.createParameter();
          p.setName("provider");
          p.setDocumentation("");
          p.setRequired(false);
          type.getParameters().add(p);

          getContents().add(type);
          break;
        }

        case TYPEFRAGMENT: {
          for(PPTypeInfo type : helper.getTypeFragments(uri.path(), new InputStreamReader(inputStream))) {
            TypeFragment fragment = PPTPFactory.eINSTANCE.createTypeFragment();
            fragment.setName(type.getTypeName());

            // add the properties (will typically load just one).
            for(Map.Entry<String, PPTypeInfo.Entry> entry : type.getProperties().entrySet()) {
              Property property = PPTPFactory.eINSTANCE.createProperty();
              property.setName(entry.getKey());
              property.setDocumentation(entry.getValue().documentation);
              property.setRequired(entry.getValue().isRequired());
              fragment.getProperties().add(property);
            }

            // add the parameters (will typically load just one).
            for(Map.Entry<String, PPTypeInfo.Entry> entry : type.getParameters().entrySet()) {
              Parameter parameter = PPTPFactory.eINSTANCE.createParameter();
              parameter.setName(entry.getKey());
              parameter.setDocumentation(entry.getValue().documentation);
              parameter.setRequired(entry.getValue().isRequired());
              fragment.getParameters().add(parameter);
            }
            getContents().add(fragment);
          }
          break;
        }
        case IGNORED:
          break;
      }
    }
    catch(RubySyntaxException syntaxException) {
      getErrors().add(new RubySyntaxExceptionDiagnostic(syntaxException));
    }
    finally {
      helper.tearDown();
    }
  }
View Full Code Here

  @Test
  public void testParseFunctionInNestedModules() throws Exception {
    File aRubyFile = TestDataProvider.getTestFile(new Path(
      "testData/pp-modules-ruby/module-x/lib/puppet/parser/functions/echotest.rb"));
    RubyHelper helper = new RubyHelper();
    helper.setUp();
    try {
      List<PPFunctionInfo> foundFunctions = helper.getFunctionInfo(aRubyFile);
      assertEquals("Should have found one function", 1, foundFunctions.size());
      PPFunctionInfo info = foundFunctions.get(0);
      assertEquals("Should have found echotest", "echotest", info.getFunctionName());
      assertTrue("Should have been an rValue", info.isRValue());
    }
    finally {
      helper.tearDown();
    }
  }
View Full Code Here

  @Test
  public void testParseFunctionInQualifiedNamedModule() throws Exception {
    File aRubyFile = TestDataProvider.getTestFile(new Path(
      "testData/pp-modules-ruby/module-x/lib/puppet/parser/functions/echotest2.rb"));
    RubyHelper helper = new RubyHelper();
    helper.setUp();
    try {
      List<PPFunctionInfo> foundFunctions = helper.getFunctionInfo(aRubyFile);
      assertEquals("Should have found one function", 1, foundFunctions.size());
      PPFunctionInfo info = foundFunctions.get(0);
      assertEquals("Should have found echotest", "echotest2", info.getFunctionName());
      assertTrue("Should have been an rValue", info.isRValue());

    }
    finally {
      helper.tearDown();
    }
  }
View Full Code Here

  @Test
  public void testParseFunctionWithFullyQualifiedName() throws Exception {
    File aRubyFile = TestDataProvider.getTestFile(new Path(
      "testData/pp-modules-ruby/module-x/lib/puppet/parser/functions/echotest3.rb"));
    RubyHelper helper = new RubyHelper();
    helper.setUp();
    try {
      List<PPFunctionInfo> foundFunctions = helper.getFunctionInfo(aRubyFile);
      assertEquals("Should have found one function", 1, foundFunctions.size());
      PPFunctionInfo info = foundFunctions.get(0);
      assertEquals("Should have found echotest", "echotest3", info.getFunctionName());
      assertTrue("Should have been an rValue", info.isRValue());

    }
    finally {
      helper.tearDown();
    }
  }
View Full Code Here

TOP

Related Classes of com.puppetlabs.geppetto.ruby.RubyHelper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.